首页 > 编程语言 > 详细

junit springboot 跑测试时,打印出当前执行的test方法信息

时间:2020-04-23 10:28:18      阅读:226      评论:0      收藏:0      [点我收藏+]

但有时候还是需要使用junit做测试。方便日后参考。

目前流行的springboot 的junit测试,在很多时候需要使用。当前执行的方法是什么,我们只需要引入用注解方法就可以了。

pom.xml引入依赖jar包

<!-- 测试 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          </dependency>
        <dependency>
          <groupId>org.junit.platform</groupId>
          <artifactId>junit-platform-launcher</artifactId>
        </dependency>
<!--这个alibaba的json也加入下-->


<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>

<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

 

junit测试类

能打印当前方法是哪个test主要是下面这句话

 @Rule
public TestName junitClass= new TestName();

引用了lombok包后,log使用如下注解

@Log4j2
在代码中可直接使用log,就可以了,不用再使用一大串
private Logger log = LoggerFactory.getLogger(getClass());

 

完整测试类

package com.test.mq;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.alibaba.fastjson.JSON;
import com.my.cloud.SearchServerApplication;
import com.my.cloud.common.utils.RedisUtils;

import lombok.extern.log4j.Log4j2;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SearchServerApplication.class)
@Log4j2
public class RedisTest {

    
    private Long starttime;
    @Rule
    public TestName junitClass= new TestName();
    @Before
    public void before() {
        starttime = System.currentTimeMillis();
        System.out.println(junitClass.getMethodName() + "....................start....................");
    }
    @After
    public void after() {
        double usedtime = (System.currentTimeMillis() - starttime) / 1000.0;
        System.out.println("耗时  " + usedtime + " ms");
        System.out.println(junitClass.getMethodName() + "....................end....................");
    }

    @Test
    public void redisSet() {
        log.info("redisSet测试");
        String redisKey = "search_redis_key";
        System.out.println("===========================");
        
        RedisUtils.set(redisKey, "当前时间:10000009610028");

        String redisValue = RedisUtils.get(redisKey);
        System.out.println("查询结果为:" + redisValue);
        
        log.info("发送完成");
    }
}

 

运行结果

可以看到已经打印出当前执行的方法是methodtest

2020-04-23 10:06:58.558  INFO [ms-server-search,,,] 51088 --- [           main] com.test.mq.CmsDicServiceTest            : Started CmsDicServiceTest in 65.613 seconds (JVM running for 68.844)
methodtest....................start....................
2020-04-23 10:06:59.361  INFO [ms-server-search,,,] 51088 --- [           main] com.test.mq.CmsDicServiceTest            : methodtest测试
耗时  0.008 ms
methodtest....................end....................

技术分享图片

 

junit springboot 跑测试时,打印出当前执行的test方法信息

原文:https://www.cnblogs.com/a393060727/p/12758944.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!