Junit springboot打印測試方法信息
有時候需要使用junit做測試。方便日后參考。
目前流行的springboot 的junit測試,在很多時候需要使用。當前執(zhí)行的方法是什么,我們只需要引入用注解方法就可以了。
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....................
可以看到已經(jīng)打印出當前執(zhí)行的方法是methodtest

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java.try catch finally 的執(zhí)行順序說明
這篇文章主要介紹了Java.try catch finally 的執(zhí)行順序說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
springboot基于docsify?實現(xiàn)隨身文檔
這篇文章主要介紹了springboot基于docsify實現(xiàn)隨身文檔的相關資料,需要的朋友可以參考下2022-09-09
解決nacos報錯java.lang.ClassNotFoundException: com.netflix.
這篇文章主要介紹了解決nacos報錯java.lang.ClassNotFoundException: com.netflix.config.DynamicPropertyFactory的問題,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
Java中如何將?int[]?數(shù)組轉(zhuǎn)換為?ArrayList(list)
這篇文章主要介紹了Java中將?int[]?數(shù)組?轉(zhuǎn)換為?List(ArrayList),本文通過示例代碼給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
pom.xml中解決Provides?transitive?vulnerable?dependency?mave
這篇文章主要介紹了在pom.xml中如何解決Provides?transitive?vulnerable?dependency?maven:org.yaml:snakeyaml:1.33警告問題,需要的朋友可以參考下2023-06-06
Spring Boot中的那些條件判斷的實現(xiàn)方法
這篇文章主要介紹了Spring Boot中的那些條件判斷的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04

