【问题标题】:@AutoConfigureMockMvc fails integration tests with inifnite loop@AutoConfigureMockMvc 无法通过无限循环进行集成测试
【发布时间】:2022-07-04 16:41:54
【问题描述】:

我们有一个集成测试,比如下面那个曾经有效的测试:

@ActiveProfiles("local")
@WithMockUser("j_unit_user_http_test")
@RunWith(SpringRunner.class)
@SpringBootTest(
    classes = { Application.class },
    webEnvironment = RANDOM_PORT
)
@Transactional
@Rollback
@AutoConfigureMockMvc()
public abstract class HttpTest {

    static {
        //reads and sets a dependency related props
        PropertiesReader propertiesReader = new PropertiesReader();
        propertiesReader.readDependencyProperties().forEach(System::setProperty);
    }

    @Autowired
    private MockMvc mockMvc;

    @PersistenceContext
    private EntityManager em;


    @Test
    public void createDashboard() {
        // POST is a utility method that wraps "mockMvc.perform(post(url))", I've omitted it here for brevity.
        var postResult = POST("/api/dashboards", Map.of("name", "wonderland"));
        var newDashboard = extractJson(postResult);
        assertTrue(newDashboard.get("id").isInt());
    }

}

在我们所做的更改中,似乎导致错误的重要更改包括:

  1. 将 spring-boot 从“2.3.0”升级到“2.5.6”
  2. 在应用的静态 void 主类中设置一些依赖项所需的环境属性:
public class Application {

    public static void main(String[] args) {
        // reads and sets dependency related props
        PropertiesReader propertiesReader = new PropertiesReader();
        propertiesReader.readDependencyProperties().forEach(System::setProperty);
    }
}

我们得到的错误是:

java.lang.StackOverflowError
    at java.base/java.lang.Throwable.getOurStackTrace(Throwable.java:828)
    at java.base/java.lang.Throwable.getStackTrace(Throwable.java:820)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:55)
    at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60) // several frames of this follow

我的猜测是 mockMvc 配置不正确,因为如果我在没有 @SpringBootTest 和 @AutoConfigureMvc 的情况下使用它,测试就会起作用。知道有什么问题吗?

【问题讨论】:

    标签: integration-testing spring-boot-test mockmvc logback-classic


    【解决方案1】:

    上述问题已修复。

    在测试开始失败时的提交中,除了其他更改之外,spring-boot 版本从 2.3.x 更改为 2.5.x 结果在 2.4 版 spring-boot removed JUnit 5's vintage engine.

    正如他们在发行说明中指出的那样,理想情况下,我应该将测试迁移到 junit 5,但与此同时,在 build.gradle 中添加以下内容会有所帮助:

    testImplementation("org.junit.vintage:junit-vintage-engine") {
        exclude group: "org.hamcrest", module: "hamcrest-core"
    }
    

    pom.xml 中的等效更改为:

    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      相关资源
      最近更新 更多