IntelliJ 2018.1 Spring Boot集成测试问题

时间:2018-04-09 08:15:10

标签: spring-boot intellij-idea

我将IntelliJ版本从2017.3升级到2018.1,现在我无法再从IDE运行集成测试。

该项目是一个Spring Boot应用程序,使用gradle构建。集成测试基本上看起来像这样:

@RunWith(SpringRunner.class)
@SpringBootTest(
    classes = TestApplication.class,
    webEnvironment = SpringBootTest.WebEnvironment.NONE
)
@Transactional
public class MyServiceIT {

    @Autowired
    private MyService service;

    @Test
    public void test() {
        // ...
    }
}

仍然可以使用gradle运行测试,但我无法从IDE运行隔离测试。

我收到此错误,因为实例无法再自动连接:

[INFO] org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [MyServiceIT]: no resource found for suffixes {-context.xml, Context.groovy}.
[INFO] org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
[INFO] org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@309e345f, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@56a6d5a6, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@18ce0030, org.springframework.test.context.support.DirtiesContextTestExecutionListener@4445629, org.springframework.test.context.transaction.TransactionalTestExecutionListener@45b9a632, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@25d250c6, org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener@4df50bcc, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@6b26e945, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@63a65a25, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@54c562f7, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@318ba8c8, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@6dbb137d, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@3c9d0b9d]

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ...

我与2017.1版完美搭配。我想我必须在IDE配置中改变一些东西,但我不知道是什么。

我使用的是Windows 10,IntelliJ 2018.1,Java 1.8和Spring Boot 1.5.10.RELEASE

2 个答案:

答案 0 :(得分:0)

根据我的经验,在这样的情况下,测试使用Gradle,而不是IntelliJ,它大部分时间都是因为类不同步。这应该可以通过重建项目来解决:

Build -> Rebuild Project

在特别顽固的情况下,可能有助于额外清除缓存:

File -> Invalidate Caches / Restart...

答案 1 :(得分:0)

最后,我让它再次运行。在Run/Debug Configurations中,Working directory设置为$MODULE_DIR$。在我的项目中没有正确解决。将Working directory显式设置为项目根目录后,运行特定的集成测试可以正常工作。