I am very new in Java Unit Testing and I heard that Mockito framework is really good for testing purposes.
I have developed a REST Server (CRUD methods) and now I want to test it, but I don’t know how?
Even more I don’t know how this testing procedure should begin. My server should work on localhost and then make calls on that url(e.g. localhost:8888)?
Here is what I tried so far, but I’m pretty sure that this isn’t the right way.
@Test
public void testInitialize() {
RESTfulGeneric rest = mock(RESTfulGeneric.class);
ResponseBuilder builder = Response.status(Response.Status.OK);
builder = Response.status(Response.Status.OK).entity(
"Your schema was succesfully created!");
when(rest.initialize(DatabaseSchema)).thenReturn(builder.build());
String result = rest.initialize(DatabaseSchema).getEntity().toString();
System.out.println("Here: " + result);
assertEquals("Your schema was succesfully created!", result);
}
Here is the code for initialize method.
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("/initialize")
public Response initialize(String DatabaseSchema) {
/** Set the LogLevel to Info, severe, warning and info will be written */
LOGGER.setLevel(Level.INFO);
ResponseBuilder builder = Response.status(Response.Status.OK);
LOGGER.info("POST/initialize - Initialize the " + user.getUserEmail()
+ " namespace with a database schema.");
/** Get a handle on the datastore itself */
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
datastore.put(dbSchema);
builder = Response.status(Response.Status.OK).entity(
"Your schema was succesfully created!");
/** Send response */
return builder.build();
}
In this test case I want to send a Json string to the server(POST). If everything went well then the server should reply with “Your schema was succesfully created!”.
Can someone please help me?
OK. So, the contract of the method is the following: Parse the input string as JSON, and send back
BAD_REQUESTif it’s invalid. If it’s valid, create an entity in thedatastorewith various properties (you know them), and send backOK.And you need to verify that this contract is fulfilled by the method.
Where does Mockito help here? Well, if you test this method without Mockito, you need a real
DataStoreService, and you need to verify that the entity has been created correctly in this realDataStoreService. This is where your test is not a unit test anymore, and this is also where it’s too complex to test, too long, and too hard to run because it needs a complex environment.Mockito can help by mocking the dependency on the
DataStoreService: you can create a mock ofDataStoreService, and verify that this mock is indeed called with the appropriate entity argument when you call yourinitialize()method in your test.To do that, you need to be able to inject the
DataStoreServiceinto your object under test. It can be as easy as refactoring your object in the following way:And now in your test method, you can do: