使用SpringBoot2.x的单元测试

DBC 1.7K 0
引入相关依赖
<!--springboot程序测试依赖,如果是自动创建项目默认添加-->
 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>


<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
</dependency>

 

 

 

搞一个测试类
import com.example.demo.AndroidApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)  //底层用junit  SpringJUnit4ClassRunner
@SpringBootTest(classes={AndroidApplication.class})//启动整个springboot工程
public class Mytest {

    @Test
    public void test1(){
        System.out.println("你好,这里是测试方法");
    }
}
温馨提示

这里一定要记住,如果是在微服务下,pom包一定要非常仔细,不要在主pom里面导入了,然后又在common的pom里面导入,又在你的子项目比如user的pom里面导入,这里搞来搞去可能会出现包的冲突,解决方法,基本删得只剩下一个之后重启编译器就可以实现了!

常用单元测试的注解

@before
@Test
@After

发表评论 取消回复
表情 图片 链接 代码

分享