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主要是下面這句話
@Rulepublic TestName junitClass= new TestName();
引用了lombok包后,log使用如下注解
@Log4j2
在代碼中可直接使用log,就可以了,不用再使用一大串
private Logger log = LoggerFactory.getLogger(getClass());
完整測(cè)試類
@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('耗時(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 mymethodtest....................end....................
可以看到已經(jīng)打印出當(dāng)前執(zhí)行的方法是methodtest
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. HTML中的XML數(shù)據(jù)島記錄編輯與添加2. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法3. ASP常用日期格式化函數(shù) FormatDate()4. .NET 中配置從xml轉(zhuǎn)向json方法示例詳解5. 一文掌握在Vue3中書寫TSX的使用方法6. 以PHP代碼為實(shí)例詳解RabbitMQ消息隊(duì)列中間件的6種模式7. 推薦一個(gè)好看Table表格的css樣式代碼詳解8. chat.asp聊天程序的編寫方法9. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?10. 如何使用瀏覽器擴(kuò)展篡改網(wǎng)頁(yè)中的JS 文件
