SpringBoot测试类

//不加这两个注解,所有自动注入的对象将会失败(null)
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

}

错误一

  • 报错

junit启动报错 Unable to find a @SpringBootConfiguration

  • 原因

项目SpringBoot启动类路径为com.eddue.datav下与测试类com.eddue.biz不一致。因为@SpringBootTest会自动查询@SpringBootConfiguration,在com.eddue.biz类路径下搜索@SpringBootConfiguration。然而java源代码的类路径为com.eddue.datav下的ServerApplication的@SpringBootApplication中引用了@SpringBootConfiguration,这里才有@SpringBootConfiguration注解。因此导致无法查询到@SpringBootConfiguration注解,导致无法加载springboot启动类ServerApplication。

  • 解决方案:

1、修改测试类和启动类的包名为一致的
2、修改测试类代码,将测试类代码@SpringBootTest改为@SpringBootTest(classes = XXXApplication.class),手动指定springboot启动类路径。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值