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)文章
詳解SpringMVC重定向傳參數(shù)的實(shí)現(xiàn)
本篇文章主要介紹了詳解SpringMVC重定向傳參數(shù)的實(shí)現(xiàn),我們可以使用重定向的方式,改變?yōu)g覽器的地址欄,防止表單因?yàn)樗⑿轮貜?fù)提交。有興趣的可以了解一下。2017-01-01springboot?無(wú)法自動(dòng)裝配的問(wèn)題
這篇文章主要介紹了springboot?無(wú)法自動(dòng)裝配的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01maven報(bào)錯(cuò):Failed to execute goal on p
這篇文章主要介紹了maven報(bào)錯(cuò):Failed to execute goal on project問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04不規(guī)范使用ThreadLocal導(dǎo)致bug分析解決
這篇文章主要為大家介紹了不規(guī)范使用ThreadLocal導(dǎo)致bug分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01