I need your help with PowerMock and some REST Testing.
I want to test a REST GET all, which is returning data from a database via a different package. Therefor, the REST method calls a executionService.getData();
@Path("interface/more/path/")
public class RestConnection {
[..]
@GET
@Path("path/{ID}/")
@Produces(MediaType.APPLICATION_JSON)
public Response showDetails(@PathParam("ID") int ID) {
if (something == 0) {
LinkedList<Integer> scenarioIDs = executionService.getData();
[..]
which calls a method from a different package for the lookup.
public class ExecutionService {
[..]
public LinkedList<Integer> getData() {
return dbLookup.getDataIDs(); //which makes a lookup in MySql
}
In my test i want to mockup getData() so that i doesnt perform the db lookup rather just return a pre prepared linkedlist which is json encoded with the rest and returned.
@PowerMockIgnore({"javax.net.ssl.*","java.sql.*","com.mysql.jdbc.*"})
@RunWith(PowerMockRunner.class)
@PrepareForTest(ExecutionService.class)
public class RestConnectionTest {
private ExecutionService executionService = new ExecutionService();
@Before
public void setUp() {
ExecutionService executionService = PowerMock.createPartialMock(ExecutionService.class,
"getData");
}
@Test
public void testGetScenarios() {
suppress(constructor(Connection.class));
String getUrl = "/interface/more/path/path/0/";
try {
//PowerMock.mockStatic(ExecutionService.class);
// PowerMock.replay(ExecutionService.class);
PowerMock.expectPrivate(executionService, "getData").andAnswer(new IAnswer<LinkedList>() {
@Override
public LinkedList answer() throws Throwable {
LinkedList ll = new LinkedList();
ll.add(1);
ll.add(2);
ll.add(3);
return (ll);
}
});
//PowerMock.verify(ExecutionService.class);
} catch (Exception e) {
e.printStackTrace();
}
get(getUrl).
then().
assertThat().
body(
matchesJsonSchemaInClasspath("json/schema.json")
);
}
now.. somehow i get a
IllegalStateException - no last call on a mock available
Error and a
java.net.ConnectException: Connection refused
Error. Further it seems, that PowerMock executes the original getData() method which tries to connect to the database. And that fails also. Could you help me with that?
Thanks!
Aucun commentaire:
Enregistrer un commentaire