代码之家  ›  专栏  ›  技术社区  ›  Rod McCutcheon

测试Spring云配置服务器

  •  0
  • Rod McCutcheon  · 技术社区  · 4 年前

    测试Spring Cloud Config Server有什么最佳实践吗?

    我发现在部署到Kubernetes时很难正确配置,每次修复和重新部署都需要时间。当进行更改时,还存在回归失败的问题。

    首先,是否有任何方法可以编写应用程序上下文将加载的冒烟测试?与此类似:

    @SpringBootTest
    @TestPropertySource(properties = { "spring.config.location=classpath:application.yml" })
    @Tag("Smoke")
    public class TellusIngestionApplicationTests {
    
        @Test
        void contextLoads() {
        }
    
    }
    

    但是,与其加载测试应用程序.yml文件,不如从Spring Cloud config Server加载配置文件,并测试不同的配置文件?例如开发阶段、生产等。

    0 回复  |  直到 4 年前
        1
  •  0
  •   Rod McCutcheon    4 年前

    感谢Spencer提供的示例代码!

    为了适应JUnit5,我必须在应用程序上下文启动之前编写一个自定义扩展来启动配置服务器(@BeforeAll似乎不起作用)。我最终得到了以下代码:

    import com.example.configserver.ConfigServerApplication;
    import org.junit.jupiter.api.Nested;
    import org.junit.jupiter.api.Tag;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.AfterAllCallback;
    import org.junit.jupiter.api.extension.BeforeAllCallback;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.junit.jupiter.api.extension.ExtensionContext;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.test.context.ActiveProfiles;
    
    import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
    
    @ExtendWith(ClockApplicationTests.ConfigServerExtension.class)
    @SpringBootTest(classes = ClockApplication.class,
            // Normally spring.cloud.config.enabled:true is the default but since we have the config server on the classpath
            // we need to set it explicitly.
            properties = {
                "spring.cloud.config.enabled:true",
                "management.security.enabled=false",
                "management.endpoints.web.exposure.include=*"
            },
            webEnvironment = RANDOM_PORT)
    @Tag("Smoke")
    class ClockApplicationTests {
    
        static class ConfigServerExtension implements BeforeAllCallback, AfterAllCallback {
    
            @Override
            public void beforeAll(ExtensionContext extensionContext) {
                if (server == null) {
                    server = new SpringApplicationBuilder(ConfigServerApplication.class)
                            .run("--server.port=" + CONFIG_PORT,
                                    "--spring.cloud.config.server.git.uri=???",
                                    "--spring.cloud.config.server.git.username=???",
                                    "--spring.cloud.config.server.git.password=???",
                                    "--spring.cloud.config.server.git.default-label=master",
                                    "--spring.cloud.config.server.git.search-paths=???");
                }
            }
    
            @Override
            public void afterAll(ExtensionContext extensionContext) {
                if (server != null) {
                    server.close();
                }
            }
        }
    
        private static final int CONFIG_PORT = 8888;
    
        private static ConfigurableApplicationContext server;
    
        @Nested
        @ActiveProfiles("docker")
        @Tag("docker")
        class Docker {
    
            @Test
            void contextLoads() {
                // The application context will fail to load if the required properties are not found
            }
    
        }
    
        @Nested
        @ActiveProfiles("kubernetes")
        @Tag("kubernetes")
        class Kubernetes {
    
            @Test
            void contextLoads() {
                // The application context will fail to load if the required properties are not found
            }
    
        }
    
    }
    

    注意:这假设您没有为属性设置默认值。或者,您可以断言属性的值,类似于Spencer的示例代码。