Junit springboot打印测试方法信息
2020-04-27 12:03
1186 查看
有时候需要使用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主要是下面这句话
@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
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
相关文章推荐
- SpringBoot中Junit测试注入Bean失败的解决方法
- Springboot 打包跳过Junit测试
- spring 或springboot对controller层方法进行测试
- springboot + mybatis +junit 测试用例编写
- SpringBoot建立父pom和JUnit代码测试
- SpringBoot开发Junit单元测试方法
- springboot下配置junit测试环境
- Spring-boot单元测试(私有方法测试)
- Spring Boot JUnit 测试类执行Cotroller操作
- Spring Boot教程(十)异步方法测试
- spring boot测试打包部署的方法
- SpringBoot 使用junit 进行测试类的编写
- Spring注解编程时用junit测试时,显示jdK版本过低问题,更改版本后仍然不行的解决方法
- Spring boot 中的事务管理,并基于junit进行测试
- springboot junit 测试出现的错误
- Springboot+Junit 模拟测试 Get请求
- 基于JUnit测试Spring MVC的正确方法。(仅限Spring 3.2)
- Spring基础:快速入门spring boot(8):使用Junit进行测试
- SpringBoot中的Junit测试
- springboot 中使用 AOP 打印 类名、方法名、方法参数名称和方法参数值