junit springboot 跑測試時,打印出當前執行的test方法信息


但有時候還是需要使用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() + "測試");
    }
}

 

運行結果

可以看到已經打印出當前執行的方法是methodtest

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....................

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM