一. 測試一般程式(Service/DAO/Util類)
1. 在pom.xml中引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
複製程式碼
2. 生成測試類
<1> 如果使用IntelliJ IDEA,可以使用快捷鍵直接生成:
- Windows快捷鍵:Ctrl + Shift + T
- Mac快捷鍵:Commond + Shift + T
<2> 自己手動去建立
3. 編寫測試類
<1> 在測試類上加入@RunWith(SpringRunner.class) 與@SpringBootTest 註解
<2> 編寫測試方法並新增@Test註解
二. 測試Controller類
1. 使用TestRestTemplate物件測試
<1> 在pom.xml中引入依賴(與上相同)
<2> 在測試類上加入@RunWith(SpringRunner.class) 與 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 註解
<3> 使用TestRestTemplate物件測試
2. 使用@WebMvcTest 註解測試
<1> 在pom.xml中引入依賴(與上相同)
<2> 在測試類上加入@RunWith(SpringRunner.class) 與 @WebMvcTest 註解
<3> 使用MockMvc物件測試
仍然測試UserController類
使用總結及相關注意點
1. @WebMvcTest 與 @SpringBootTest 註解不能一起使用,會報錯。
錯誤資訊:found multiple declarations of @BootstrapWith
一個是:(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper)
一個是:(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.example.demo.controller.UserControllerTest2]: [@org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]
at org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper(BootstrapUtils.java:166)
at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:127)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:124)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:151)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:142)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:36)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
複製程式碼
2. 使用@WebMvcTest註解進行測試時,只會載入在@WebMvcTest()中配置的bean,而@SpringBootTest註解會載入所有被Spring容器管理的bean
例如:@WebMvcTest(UserController.class) 只會載入UserController類