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() + "測試");
}
}
運行結(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測試 耗時 0.008 my methodtest....................end....................
可以看到已經(jīng)打印出當前執(zhí)行的方法是methodtest

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解SpringMVC重定向傳參數(shù)的實現(xiàn)
本篇文章主要介紹了詳解SpringMVC重定向傳參數(shù)的實現(xiàn),我們可以使用重定向的方式,改變?yōu)g覽器的地址欄,防止表單因為刷新重復提交。有興趣的可以了解一下。2017-01-01
maven報錯:Failed to execute goal on p
這篇文章主要介紹了maven報錯:Failed to execute goal on project問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
不規(guī)范使用ThreadLocal導致bug分析解決
這篇文章主要為大家介紹了不規(guī)范使用ThreadLocal導致bug分析解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01

