歡迎您光臨本站 註冊首頁

Junit springboot打印測試方法信息

←手機掃碼閱讀     月球人 @ 2020-05-05 , reply:0

目前流行的springboot 的junit測試,在很多時候需要使用。當前執行的方法是什麼,我們只需要引入用註解方法就可以了。
pom.xml引入依賴jar包

org.springframework.bootspring-boot-starter-testtestorg.junit.jupiterjunit-jupiter-engineorg.junit.platformjunit-platform-launchercom.alibabafastjson1.2.62org.projectlomboklombokprovided


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() + "測試"); } }
運行結果
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....................
可以看到已經打印出當前執行的方法是methodtest


[月球人 ] Junit springboot打印測試方法信息已經有251次圍觀

http://coctec.com/docs/java/show-post-232964.html