SpringBoot集成测试

一. 测试一般程序(Service/DAO/Util类)

  1. 在pom.xml中引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
  1. 生成测试类
    <1> 如果使用IntelliJ IDEA,可以使用快捷键直接生成:

Windows快捷键:Ctrl + Shift + T
Mac快捷键:Commond + Shift + T
<2> 自己手动去创建
在这里插入图片描述
Ctrl + Shift + T生成测试类
在这里插入图片描述
Ctrl + Shift + T生成测试类
在这里插入图片描述
测试类展示

  1. 编写测试类
    <1> 在测试类上加入@RunWith(SpringRunner.class) 与@SpringBootTest 注解,
    <2> 编写测试方法并添加@Test注解
    在这里插入图片描述
    UserServiceImplTest测试类

二. 测试Controller类

  1. 使用TestRestTemplate对象测试
    <1> 在pom.xml中引入依赖(与上相同)
    <2> 在测试类上加入@RunWith(SpringRunner.class) 与 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 注解
    <3> 使用TestRestTemplate对象测试
    在这里插入图片描述
    Controller类
    在这里插入图片描述
    使用TestRestTemplate测试Controller类

  2. 使用@WebMvcTest 注解测试
    <1> 在pom.xml中引入依赖(与上相同)
    <2> 在测试类上加入@RunWith(SpringRunner.class) 与 @WebMvcTest 注解
    <3> 使用MockMvc对象测试

仍然测试UserController类
在这里插入图片描述
使用@WebMvcTest注解测试
使用总结及相关注意点

  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)
  1. 使用@WebMvcTest注解进行测试时,只会加载在@WebMvcTest()中配置的bean,而@SpringBootTest注解会加载所有被Spring容器管理的bean
    例如:@WebMvcTest(UserController.class) 只会加载UserController类

  2. 如果使用MockMvc对象时,又希望会加载所有被Spring容器管理的bean,可以使用@AutoConfigureMockMvc注解
    在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值