首页 > 解决方案 > 不使用弹簧启动的测试中的 DI (@SpringBootTest)

问题描述

从 spring boot 切换回“正常” spring,因为该应用程序仅使用一些 jdbc 代码“upsert”到 postgresql 数据库中。

1)
尝试用以下方法注释测试类:

@RunWith(SpringJUnit4ClassRunner.class)
public class DBIntegration {

results in:
java.lang.IllegalStateException: Failed to load ApplicationContext

2)
尝试用以下方法注释类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {})
public class DBIntegration {

[main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [de.mydomain.myproject.DBIntegration]: no resource found for suffixes {-context.xml}.

No exceptions, but java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=insertDataFrom_sometest], 

3)尝试用以下方法注释类:

@Component
public class DBIntegration {

dependency injection does not work in this case, the expected service
(to be injected) throws a nullpointerexception

标签: spring

解决方案


推荐阅读