我是 Java 单元测试的新手,听说 Mockito 框架非常适合测试。
我已经开发了一个 REST 服务器(CRUD 方法),现在我想测试它,但我不知道怎么做?
更我不知道这个测试程序应该如何开始。我的服务器应该在 localhost 上运行,然后在该 url 上进行调用(例如 localhost:8888)?
这是我到目前为止所尝试的,但我很确定这不是正确的方法。
@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);
}
这是initialize
方法的代码。
@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();
}
在这个测试用例中,我想将一个 Json 字符串发送到服务器(POST)。如果一切顺利,那么服务器应该回复“您的架构已成功创建!”。
有人可以帮帮我吗?