Jak używać mockito do testowania usługi REST?
Jestem bardzo nowy w Java Unit Testing i słyszałem, że framework Mockito jest naprawdę dobry do celów testowych.
Opracowałem serwer REST (metody CRUD) i teraz chcę go przetestować, ale nie wiem jak?
Co więcej, nie wiem, jak powinna się rozpocząć ta procedura testowania. Mój serwer powinien działać na localhost, a następnie wykonywać połączenia na tym adresie URL (np. Localhost: 8888)?
Oto, co próbowałem do tej pory, ale jestem pewien, że nie jest to właściwy sposób.
@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);
}
Oto kod dlainitialize
metoda.
@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();
}
W tym przypadku testowym chcę wysłać ciąg Json na serwer (POST). Jeśli wszystko poszło dobrze, serwer powinien odpowiedzieć „Twój schemat został pomyślnie utworzony!”.
Czy ktoś może mi pomóc?