Junit springboot打印测试方法信息
有时候需要使用junit做测试。方便日后参考。
目前流行的springboot 的junit测试,在很多时候需要使用。当前执行的方法是什么,我们只需要引入用注解方法就可以了。
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主要是下面这句话
@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() + '测试'); }}
运行结果
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....................
可以看到已经打印出当前执行的方法是methodtest
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。
相关文章:
1. js select支持手动输入功能实现代码2. PHP正则表达式函数preg_replace用法实例分析3. vue使用moment如何将时间戳转为标准日期时间格式4. Android studio 解决logcat无过滤工具栏的操作5. vue-drag-chart 拖动/缩放图表组件的实例代码6. 什么是Python变量作用域7. Android 实现彻底退出自己APP 并杀掉所有相关的进程8. bootstrap select2 动态从后台Ajax动态获取数据的代码9. Android Studio3.6.+ 插件搜索不到终极解决方案(图文详解)10. 一个 2 年 Android 开发者的 18 条忠告
