首页 > 其他分享 >Spring-Boot-19-Junit测试

Spring-Boot-19-Junit测试

时间:2023-07-12 14:33:01浏览次数:46  
标签:19 Spring void After Boot test public 测试方法 Before


  1. 随着Spring开发的深入,我们逐渐打算使用Spring-test与Junit结合进行开发测试

1. Jar形式的Maven依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.0.6.RELEASE</version>
</dependency>

2. 用来加载的Spring的上下文环境

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BaseTest {
    //如此用来加载环境
    protected MockMvc mockMvc;
    @Autowired
    protected WebApplicationContext wac;
    @Before()  //这个方法在每个方法执行之前都会执行一遍
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();  //初始化MockMvc对象
    }
}

3. 正常的测试类的形式和实现

  1. 继承自BaseTest,然后进行正常的测试

3.1. 非Controller部分的测试检测

  1. 直接使用@AutoWired来自动装载对应测试部分

3.2. Controller部分的测试

  1. 我们使用MockMvc来完成
  2. 加载参数可以
  1. 通过JSON格式转换(使用.content(string))方法完成
  2. 通过param来一个一个处理。
@RunWith(SpringJUnit4ClassRunner.class)  
@WebAppConfiguration  
@ContextConfiguration({"classpath*:/beans.xml","classpath*:/spring-mvc.xml"}) 
//当然 你可以声明一个事务管理 每个单元测试都进行事务回滚 无论成功与否  
@TransactionConfiguration(defaultRollback = true)  
@Transactional 
public class TestController extends BaseTest{
    @Test  
    public void testLogin() throws Exception {  
        mockMvc.perform((post("/loginTest").param("userName", "admin").param("password", "1"))).andExpect(status().isOk()).andDo(print());
        //进行处理之后的检测
    }
}

4. Junit中出现的问题汇总

4.1. @Before 和 @After 注解部分不执行

//问题代码
@Before
public void init() {
	System.out.println("init");
}
@After
public void destroy(){
	System.err.println("destroy");
}
@Test
public void test() {
	System.out.println("test");
}

Spring-Boot-19-Junit测试_单元测试

  1. 在正常执行的时候,并没有执行@Before和@After的代码
  2. 在Junit5 中已经不存在@Before和@After注释的方法,对应的方法为 @BeforeEach 和 @AfterEach。

4.2. 总结对比@Before,@BeforeClass,@BeforeEach,@BeforeAll

特性

Junit4

Junit5

1. 在当前类的所有测试方法之前执行。

2. 注解在静态方法上。

3. 此方法可以包含一些初始化代码。

@BeforeClass

@BeforeAll

1. 在当前类中的所有测试方法之后执行。

2. 注解在静态方法上。

3. 此方法可以包含一些清理代码。

@AfterClass

@AfterAll

1. 在每个测试方法之前执行。

2. 注解在非静态方法上。

3. 可以重新初始化测试方法所需要使用的类的某些属性。

@Before

@BeforeEach

1. 在每个测试方法之后执行。

2. 注解在非静态方法上。

3. 可以回滚测试方法引起的数据库修改。

@After

@AfterEach

5. 使用Mock完成单元测试

参见参考3

6. 相关代码参考

https://github.com/spricoder/TestDemo

7. 参考

  1. JUnit5 @Before @After 注解部分不执行
  2. @Before, @BeforeClass, @BeforeEach 和 @BeforeAll之间的不同
  3. 教你使用Mock完成单元测试


标签:19,Spring,void,After,Boot,test,public,测试方法,Before
From: https://blog.51cto.com/u_16038001/6699887

相关文章

  • springboot 自定义整合caffeine 本地缓存
    1、自定义缓存配置类@Data@ConfigurationProperties(prefix="page.cache")publicclassPageCacheProperties{privateCaffeineConfigPropertiescaffeine=newCaffeineConfigProperties();//本地缓存配置privatePageCacheAsyncExecutorConfigpool=newP......
  • SpringBoot与Freemarker整合
    1.需要导入freemarker的pom文件;<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>2.需要在application.properties配置文件中配置一些freemarker的参数;serve.port......
  • upload-labs靶场1-19关详解
    upload-labs靶场下载地址https://gitcode.net/mirrors/tj1ngwe1/upload-labs?utm_source=csdn_github_accelerator需要新建一个upload文件夹,该靶场在php5.2.17版本下(除特殊说明的情况下)。Pass-01(前端验证绕过)先上传一个php文件看一下回显然后上传一个正常文件,发现回显正......
  • 7DGroup性能&测试开发文章持续更新(2019/10/15)
    性能闲谈系列:浅谈window桌面GUI技术及图像渲染性能测试实践杂谈:性能测试的范围到底有多大?戏说CPU使用率-驳《CPU使用率度量指标是扯淡!》译文标题对性能测试评估分析优化市场的反思泛谈系统级跟踪和应用级跟踪性能测试分析优化该有的范围期待996ICU的条款尽早加入到开源协议中!性能基......
  • ROS 的三种通信方式 2a82329219bf47c9a8f48a534ab31af7
    ROS的三种通信方式注意以下所有代码均基于:ubuntuROSDate18.04LTSMelodicMoreniaMay23rd,2018 May,2023写在最前,ROS1主要有三种通信方式,分别是:话题通信服务通信参数通信话题通信(Topic)话题通信主要是指通过发布和订阅服务的方式,来进行匿名的通信,一方......
  • 19:vue3 依赖注入
    1、通过Prop逐级透传问题(传统老的方法只能逐级传递) 传统方式代码如下:App.vue1<template>2<h3>祖宗</h3>3<Parent:msg="msg"></Parent>4</template>56<script>7importParentfrom"./components/Parent.vue"......
  • 二维码简易实现 Vue+Springboot
    Vue:<template><div><img:src="database64"width="150px"/><div>注:请使用手机微信扫码,并于2分钟内绑定员工账号(二维码为账号独属,请勿分享)。</div></div></template><script>import{getQrCode}from"......
  • 4-基于SpringBoot实现SSMP整合
    1.整合JunitSpring整合JUnit的制作方式//加载spring整合junit专用的类运行器@RunWith(SpringJUnit4ClassRunner.class)//指定对应的配置信息@ContextConfiguration(classes=SpringConfig.class)publicclassAccountServiceTestCase{//注入你要测试的对象......
  • spring cloud 上nacos理解
    erueka和nacos的区别:1、CAP理论的区别;2、连接方式不同;3、服务异常剔除区别;4、操作实例方式不同;5、自我保护机制不同。CAP理论中C代表一致性,A表示高可用,P代表分区容错性。eureka只支持AP,nacos支持CP和AP两种。 1CAP理论的区别CAP理论:C一致性,A高可用,P分区容错性。eurek......
  • Spring AOP、拦截器、过滤器的区别
    一:区别与概念   Filter过滤器:拦截web访问url地址。   Interceptor拦截器:拦截以.action结尾的url,拦截Action的访问 (控制层Controller)。   Spring AOP拦截器:只能拦截Spring管理Bean的访问(业务层Service)。   概念:   SpringAOP:   SpringAOP,......