springboot中单元测试依赖

参考:SpringBoot高版本修改为低版本时测试类报错解决

方法一、springboot低版本好像2.1.8.RELEASE以下 直接使用,不排除junit 因为排除也是要这样写。

Junit4 测试方法为什么必须用public ,类用public修饰

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

//@SpringBootTest(classes = {GulimallCouponApplication.class})
@SpringBootTest
@RunWith(SpringRunner.class) // 不写这个的话couponService为null
public class GulimallCouponApplicationTest2 {

    @Autowired
    CouponService couponService;

    @Test
     public void list() {
        System.out.println(1);
        System.out.println(couponService.queryPage(new HashMap<>()));
    }
}

方法二、高版本好像springboot版本为2.2.2.RELEASE,排除junit-vintage-engine后

测试类中不用写public和@RunWith(SpringRunner.class)都可以

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<!--<exclusions>-->
				<!--<exclusion>-->
					<!--<groupId>org.junit.vintage</groupId>-->
					<!--<artifactId>junit-vintage-engine</artifactId>-->
				<!--</exclusion>-->
			<!--</exclusions>-->
		</dependency>
@SpringBootTest
class GulimallCouponApplicationTest2 {

    @Autowired
    CouponService couponService;

    @Test
     void list() {
        System.out.println(1);
        System.out.println(couponService.queryPage(new HashMap<>()));
    }
}

版权声明:本文为weixin_45168162原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。