如何在 Spring Boot 微服务中为网关应用程序编写 JUnit 测试

问题描述 投票:0回答:1

我在 Spring Boot 微服务中为网关应用程序实现一些 JUnit 测试时遇到问题

这是如下所示的网关服务应用程序

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayServiceApplication {

    /**
     * The main method that starts the Spring Boot application.
     *
     * @param args The command-line arguments passed to the application.
     */
    public static void main(String[] args) {
        SpringApplication.run(GatewayServiceApplication.class, args);
    }

}

接下来,我尝试编写如下所示的 JUnit 测试

@SpringBootTest
class GatewayServiceApplicationTest {

    @Autowired
    private ApplicationContext context;

    @DisplayName("Application context loads without errors")
    @Test
    void contextLoads() {
        assertThat(context).isNotNull();
    }

    @DisplayName("Gateway Service starts successfully via main method")
    @Test
    void mainMethodTest() {
        GatewayServiceApplication.main(new String[]{});
    }

    @Test
    @DisplayName("Gateway Service is enabled and running")
    void gatewayServiceIsEnabledAndRunning() {
        assertThat(context.getBean(GatewayServiceApplication.class)).isNotNull();
    }

}

当我运行任何测试时,我收到如下所示的错误消息



java.lang.StackOverflowError
    at java.util.IdentityHashMap.hash(IdentityHashMap.java:295)
    at java.util.IdentityHashMap.put(IdentityHashMap.java:426)
    at java.util.Collections$SetFromMap.add(Collections.java:5461)

我该如何修复它?

如你所知,这个服务是在eureka服务器启动后启动的

我在 Spring Boot 微服务中为网关应用程序实现一些 JUnit 测试时遇到问题。

目的是在 GatewayServiceApplicationTest 中运行所有 JUnit 测试

java spring-boot junit api-gateway
1个回答
0
投票

Spring Boot应用程序的main()方法没有被覆盖 单元/集成测试,因为 Spring Boot 测试不会调用 main() 方法来启动 Spring Boot 应用程序。 由于您在 main 方法中传递了特定的另一个参数,因此可能 同样需要 Junit。 您可以参考以下链接。

如何JUnit测试Spring-Boot的Application.java 应用程序-java

© www.soinside.com 2019 - 2024. All rights reserved.