SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
根据SpringBoot版本引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
设置单元测试
注意添加以下两个注解,EduApplication是主启动类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EduApplication.class)
import org.junit.Test; @RunWith(SpringRunner.class) @SpringBootTest(classes = EduApplication.class) public class MyTest { @Autowired private EduCourseMapper courseMapper; @Test public void testdemo01(){ courseMapper.deleteById("1655898309098749953"); System.out.println("springboot单元测试"); } }
运行