Junit springboot打印測試方法信息
有時候需要使用junit做測試。方便日后參考。
目前流行的springboot 的junit測試,在很多時候需要使用。當(dāng)前執(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測試類
能打印當(dāng)前方法是哪個test主要是下面這句話
@Rulepublic TestName junitClass= new TestName();
引用了lombok包后,log使用如下注解
@Log4j2
在代碼中可直接使用log,就可以了,不用再使用一大串
private Logger log = LoggerFactory.getLogger(getClass());
完整測試類
@RunWith(SpringRunner.class)@SpringBootTest(classes = SearchServerApplication.class)@Log4j2public 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() + '測試'); }}
運(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測試耗時 0.008 mymethodtest....................end....................
可以看到已經(jīng)打印出當(dāng)前執(zhí)行的方法是methodtest
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 原生js實(shí)現(xiàn)的觀察者和訂閱者模式簡單示例2. asp讀取xml文件和記數(shù)3. JS錯誤處理與調(diào)試操作實(shí)例分析4. JS實(shí)現(xiàn)表單中點(diǎn)擊小眼睛顯示隱藏密碼框中的密碼5. python基于scrapy爬取京東筆記本電腦數(shù)據(jù)并進(jìn)行簡單處理和分析6. Python ellipsis 的用法詳解7. 在終端啟動Python時報錯的解決方案8. Python如何實(shí)現(xiàn)感知器的邏輯電路9. 基于android studio的layout的xml文件的創(chuàng)建方式10. PHP實(shí)現(xiàn)基本留言板功能原理與步驟詳解
