springboot 的测试,在Junit的基础上进行封装,首要是引入maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
其次,在src/test/java/xxxpackage/xxxApplicationTest.java中,创建测试文件,使用junit 注解即可,熟悉junit测试的下面可以忽略。
junit 注解常见的有 @Test @Before @After,@Test可以指定超时时间timeout 及异常,来验证测试case的性能或者异常,除此之外,@BeforeClass @AfterClass 的用途也会在下面的示例中的注释中一一说明
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceApplicationTests {
/**
* 启动的过程中输出beforeClass serviceImpl ,适用于全局的统一处理
*
* Method beforeClass() should be static
*
* 2019-08-20 14:56:12,702 INFO (AbstractTestContextBootstrapper.java:248)-
* Loaded default TestExecutionListener class names from location [META-INF/spring.factories]:
* [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener,
* org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener,
* org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener,
* org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener,
* org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener,
* org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener,
* org.springframework.test.context.web.ServletTestExecutionListener,
* org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener,
* org.springframework.test.context.support.DependencyInjectionTestExecutionListener,
* org.springframework.test.context.support.DirtiesContextTestExecutionListener,
* org.springframework.test.context.transaction.TransactionalTestExecutionListener,
* org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
* 2019-08-20 14:56:12,722 INFO (AbstractTestContextBootstrapper.java:177)- Using TestExecutionListeners:
* [org.springframework.test.context.web.ServletTestExecutionListener@55536d9e,
* org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@747edf66,
* org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@3d1cfad4,
* org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@62230c58,
* org.springframework.test.context.support.DirtiesContextTestExecutionListener@2cd2a21f,
* org.springframework.test.context.transaction.TransactionalTestExecutionListener@2e55dd0c,
* org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@74455848,
* org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@e7edb54,
* org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@378542de,
* org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@3738449f,
* org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@69e1dd28,
* org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@11bd0f3b]
*
* beforeClass serviceImpl (--启动的过程中输出beforeClass serviceImpl)
*/
@BeforeClass
public static void beforeClass(){
System.out.println("beforeClass serviceImpl");
}
/**
* 在每个测试方法前执行
* 一般用于初始化方法
*/
@Before
public void before(){
System.out.println("before serviceImpl");
}
/**
* 忽略执行的方法
* Ignore value: 忽略执行该测试方法时,会同时输出value值,以作忽略说明
*/
@Ignore(value = "忽略执行的测试case")
@Test
public void ignore(){
System.out.println("ignore Test serviceImpl");
}
/**
* 正常的测试方法
*/
@Test
public void contextLoads() {
System.out.println("test serviceImpl");
}
/**
* timeout = 1000L: 测试方法执行超过1000毫秒后算超时,测试将失败
* expected = Exception.class: 测试方法期望得到的异常类,如果方法执行没有抛出指定的异常,则测试失败
*
* java.lang.Exception: Unexpected exception, expected<java.lang.Exception> but was<java.lang.Error>
* Caused by: java.lang.Error: 这是一个错误
*/
@Test(timeout = 1000L,expected = Exception.class )
public void testTimeoutAndExpected(){
System.out.println("测试异常和超时");
throw new Error("这是一个错误");
//throw new RuntimeException("这是一个异常");
}
/**
* 在test 执行完后要做的事情
*/
@After
public void after(){
System.out.println("after serviceImpl");
}
/**
* java.lang.Exception: Method afterClass() should be static
*
* @Before @Test @After 全部执行完之后,开始执行
*/
@AfterClass
public static void afterClass(){
System.out.println("afterClass serviceImpl");
}
}
//输出顺序:
beforeClass serviceImpl
忽略执行的测试case
。。。。(启动过程)
。。。。。(启动过程)
before serviceImpl
测试异常和超时
after serviceImpl
java.lang.Exception: Unexpected exception, expected<java.lang.Exception> but was<java.lang.Error>
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.Error: 这是一个异常
at com.cutiyu.promotion.serviceimpl.PromotionServiceApplicationTests.testTimeoutAndExpected(PromotionServiceApplicationTests.java:98)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
... 9 more
before serviceImpl
test serviceImpl
after serviceImpl
afterClass serviceImpl
//若修改testTimeoutAndExpected 抛出RuntimeException,则输出顺序如下:
beforeClass serviceImpl
忽略执行的测试case
。。。。(启动过程)
。。。。。(启动过程)
before serviceImpl
测试异常和超时
after serviceImpl
before serviceImpl
test serviceImpl
after serviceImpl
afterClass serviceImpl
通过输出结果可知,@Before和@After 会在每个@Test 注解执行之前和之后,分别执行一次。
@BeforClass 和@AfterClass 全局只执行一次