Junit springboot打印測(cè)試方法信息
有時(shí)候需要使用junit做測(cè)試。方便日后參考。
目前流行的springboot 的junit測(cè)試,在很多時(shí)候需要使用。當(dāng)前執(zhí)行的方法是什么,我們只需要引入用注解方法就可以了。
pom.xml引入依賴jar包
<!-- 測(cè)試 -->
<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><!--這個(gè)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測(cè)試類
能打印當(dāng)前方法是哪個(gè)test主要是下面這句話
@Rule
public TestName junitClass= new TestName();
引用了lombok包后,log使用如下注解
@Log4j2
在代碼中可直接使用log,就可以了,不用再使用一大串
private Logger log = LoggerFactory.getLogger(getClass());
完整測(cè)試類
@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("耗時(shí) " + usedtime + " my");
System.out.println(junitClass.getMethodName() + "....................end....................");
}
@Test
public void methodtest() {
log.info(junitClass.getMethodName() + "測(cè)試");
}
}
運(yùn)行結(jié)果
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測(cè)試 耗時(shí) 0.008 my methodtest....................end....................
可以看到已經(jīng)打印出當(dāng)前執(zhí)行的方法是methodtest

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java.try catch finally 的執(zhí)行順序說(shuō)明
這篇文章主要介紹了Java.try catch finally 的執(zhí)行順序說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
Jmeter訪問(wèn)需要登錄的接口如何處理問(wèn)題
這篇文章主要介紹了Jmeter訪問(wèn)需要登錄的接口如何處理問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
springboot基于docsify?實(shí)現(xiàn)隨身文檔
這篇文章主要介紹了springboot基于docsify實(shí)現(xiàn)隨身文檔的相關(guān)資料,需要的朋友可以參考下2022-09-09
解決nacos報(bào)錯(cuò)java.lang.ClassNotFoundException: com.netflix.
這篇文章主要介紹了解決nacos報(bào)錯(cuò)java.lang.ClassNotFoundException: com.netflix.config.DynamicPropertyFactory的問(wèn)題,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
oracle數(shù)據(jù)庫(kù)導(dǎo)入TXT文件方法介紹
這篇文章主要介紹了oracle數(shù)據(jù)庫(kù)導(dǎo)入TXT文件方法介紹,文中向大家展示了具體代碼示例,需要的朋友可以參考下。2017-09-09
Java中如何將?int[]?數(shù)組轉(zhuǎn)換為?ArrayList(list)
這篇文章主要介紹了Java中將?int[]?數(shù)組?轉(zhuǎn)換為?List(ArrayList),本文通過(guò)示例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
pom.xml中解決Provides?transitive?vulnerable?dependency?mave
這篇文章主要介紹了在pom.xml中如何解決Provides?transitive?vulnerable?dependency?maven:org.yaml:snakeyaml:1.33警告問(wèn)題,需要的朋友可以參考下2023-06-06
Spring Boot中的那些條件判斷的實(shí)現(xiàn)方法
這篇文章主要介紹了Spring Boot中的那些條件判斷的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
java類中使用Jfreechart的簡(jiǎn)單實(shí)例
這篇文章介紹了java類中使用Jfreechart的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-08-08

