pom.xml中依赖配置:

 关于在spring-mybatis中使用junit测试时依赖注入报错的问题

spring-config.xml 和spring-mybatis.xml扫描包也没有问题。

测试类:

这里"classpath:spring/*.xml" 注意整合了mybatis的话需要将spring和mybatis的扫描配置文件都包含进去。

public class UploadHealthDataTaskTest {
    private static final String CLOUD_URL= PropertiesUtil.getInstanse().get("cloud_url");
    private ClassPathXmlApplicationContext context;
    private HealthPressureService healthPressureService;
    @Before
    public void setUp(){
        context = new ClassPathXmlApplicationContext("classpath:spring/*.xml");
        healthPressureService = (HealthPressureService) context.getBean("healthPressureService");
    }

}

debug时控制台报错:

关于在spring-mybatis中使用junit测试时依赖注入报错的问题

原因:javax.servlet包没有导入,在pom文件中加入

<servlet.api.version>3.1.0</servlet.api.version>

<!-- servlet api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>${servlet.api.version}</version>
    <scope>provided</scope>
</dependency>
即可。


相关文章: