当我应用springboot+junit5测试mybatisplus拜访数据库时,报异样了,异样如下:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test    ..........org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:145)        ... 29 more

说实话,这么给我报异样, 我挺心痛的:@SpringBootTest我曾经有了:

import com.niewj.cloud.domain.User;import com.niewj.cloud.mapper.UserMapper;import org.junit.Assert;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTestpublic 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);    }}

起因也很简略:test测试用例的包构造!

测试用例的包构造要在springboot的扫描范畴内才可,比方:
App.java(main办法所在的类)的包名为:com.niewj.cloud,那测试用例的测试类, 也放在这样的目录下就能够了!