欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Junit springboot打印测试方法信息

时间:2021-03-09 10:07:39|栏目:JAVA代码|点击:

有时候需要使用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());

完整测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SearchServerApplication.class)
@Log4j2
public class CmyDicServiceTest {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 + " my");
    System.out.println(junitClass.getMethodName() + "....................end....................");
  }
  @Test
  public void methodtest() {
    log.info(junitClass.getMethodName() + "测试");
  }
}

运行结果

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

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

上一篇:Java 封装基础知识

栏    目:JAVA代码

下一篇:深入理解Java多线程与并发编程

本文标题:Junit springboot打印测试方法信息

本文地址:http://www.codeinn.net/misctech/77276.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有