IllegalStateException:无法加载ApplicationContext

时间:2018-07-07 08:47:51

标签: java spring spring-mvc spring-boot

在我的测试包中,我有三个类。

我创建了基本配置类

/**
 * Spring configuration class for integration tests.
 */
@Configuration 
@EnableAutoConfiguration 
@ComponentScan
public class PopcornCoreTestApplication {}

和一个抽象类

/**
 * Base class to save on configuration.
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PopcornCoreTestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@TestExecutionListeners(
        {
                DependencyInjectionTestExecutionListener.class,
                DirtiesContextTestExecutionListener.class,
                TransactionalTestExecutionListener.class,
                TransactionDbUnitTestExecutionListener.class
        }
)
public abstract class DBUnitTestBase {

    @Autowired
    private UserRepository userRepository;

    /**
     * Clean out the db after every test.
     */
    @After
    public void cleanup() {
        this.userRepository.deleteAll();
    }
}

和一些示例测试以检查其是否有效

/**
 * Integration tests for UserPersistenceServiceImpl.
 */
public class UserPersistenceServiceImplIntegrationTests extends DBUnitTestBase {

    @Autowired
    private UserPersistenceService userPersistenceService;

    /**
     * Setup.
     */
   @Test
    public void setup() {
        Assert.assertThat(this.userRepository.count(), Matchers.is(0L));
    }
}

在测试运行中,他把我拒之门外

java.lang.IllegalStateException: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
...

https://stackoverflow.com/a/36608574/10033266上,我发现如果应用程序具有和我一样的注释配置,则只需添加@SpringBootTest,但是我已经在DBUnitTestBase类上方添加了此注释。

0 个答案:

没有答案