当我使用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;
@SpringBootTest
public class SampleTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println((\"----- selectAll method test ------\"));
List userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}
原因也很简单:test测试用例的包结构!
测试用例的包结构要在springboot的扫描范围内才可,比如:
App.java(main方法所在的类)的包名为:com.niewj.cloud,那测试用例的测试类, 也放在这样的目录下就可以了!