@Test注解是测试的基础,它提供了其他作用
1.指定将会抛出的异常类型,如果没有抛出异常或者抛出的一场不属于我们指定的类型,就会算是测试失败了。
@Test(expected = RuntimeException.class)
public void testGetHandlerNotDefined() {
SampleRequest request = new SampleRequest("testNotDefined");
// The following line is supposed to throw a RuntimeException
controller.getHandler(request);
}
2.测试一断代码运行时间。
@Test(timeout=100)
public void testProcessMultipleRequestsTimeout() {
Request request;
Response response = new SampleResponse();
RequestHandler handler = new SampleHandler();
for (int i = 0; i < 99999; i++) {
request = new SampleRequest(String.valueOf(i));
controller.addHandler(request, handler);
response = controller.processRequest(request);
Assert.assertNotNull(response);
Assert.assertNotSame(ErrorResponse.class, response.getClass());
}
}
3.在3.x版本中,如果我们不想启动某些测试,需要将方法名更改为非test开头的(3.X测试用例必须是以test开头)。而4版本只需添加一个注释@Ignore
1.指定将会抛出的异常类型,如果没有抛出异常或者抛出的一场不属于我们指定的类型,就会算是测试失败了。
@Test(expected = RuntimeException.class)
public void testGetHandlerNotDefined() {
SampleRequest request = new SampleRequest("testNotDefined");
// The following line is supposed to throw a RuntimeException
controller.getHandler(request);
}
2.测试一断代码运行时间。
@Test(timeout=100)
public void testProcessMultipleRequestsTimeout() {
Request request;
Response response = new SampleResponse();
RequestHandler handler = new SampleHandler();
for (int i = 0; i < 99999; i++) {
request = new SampleRequest(String.valueOf(i));
controller.addHandler(request, handler);
response = controller.processRequest(request);
Assert.assertNotNull(response);
Assert.assertNotSame(ErrorResponse.class, response.getClass());
}
}
3.在3.x版本中,如果我们不想启动某些测试,需要将方法名更改为非test开头的(3.X测试用例必须是以test开头)。而4版本只需添加一个注释@Ignore