如何启动多个启动应用程序进行端到端测试?

How to start multiple boot apps for end-to-end tests?

我想编写端到端测试来验证两个启动应用程序是否与各种配置文件一起工作。

已经有效的:

如果我手动启动授权服务器和资源服务器,测试工作正常。

我现在想做的是使用每个测试的正确配置文件自动启动和关闭经过测试的启动应用程序。

我试过了:

但是我面临配置错误的问题,因为所有资源和依赖项都以相同的共享类路径结尾...

有办法解决这个问题吗?

我也在考虑启动两个单独的 java -jar ... 进程,但是,如何确保在 2e2 单元测试之前构建经过测试的应用程序 fat-jars 运行?

当前应用程序 start/shutdown 代码示例在我对第二个应用程序具有 Maven 依赖性后立即失败:

    private Service startAuthorizationServer(boolean isJwtActive) throws InterruptedException {
        return new Service(
                AuthorizationServer.class,
                isJwtActive ? new String[]{ "jwt" } : new String[]{} );
    }

    private static final class Service {
        private ConfigurableApplicationContext context;
        private final Thread thread;

        public Service(Class<?> appClass, String... profiles) throws InterruptedException {
            thread = new Thread(() -> {
                SpringApplication app = new SpringApplicationBuilder(appClass).profiles(profiles).build();
                context = app.run();

            });
            thread.setDaemon(false);
            thread.start();
            while (context == null || !context.isRunning()) {
                Thread.sleep(1000);
            };
        }

        @PreDestroy
        public void stop() {
            if (context != null) {
                SpringApplication.exit(context);
            }
            if (thread != null) {
                thread.interrupt();
            }
        }
    }

我认为你的情况,运行 通过 docker 组合的两个应用程序可能是个好主意。 本文展示了如何使用 docker 组合图像设置一些集成测试:https://blog.codecentric.de/en/2017/03/writing-integration-tests-docker-compose-junit/

此外,请看一下来自 Martin Fowler 的 post:https://martinfowler.com/articles/microservice-testing/

我用第二种解决方案解决了问题:

  • end-to-end 测试项目除了 运行 spring-tests 和 TestRestClient
  • 所需的之外没有其他 Maven 依赖项
  • 测试配置初始化环境,运行在不同进程中mvn package在所需模块上
  • 测试用例 运行 在单独的 java -jar ... 进程中使用所选配置文件(重新)启动应用程序

这是我为此写的助手class(已被from there):

class ActuatorApp {
    private final int port;
    private final String actuatorEndpoint;
    private final File jarFile;
    private final TestRestTemplate actuatorClient;
    private Process process;

    private ActuatorApp(File jarFile, int port, TestRestTemplate actuatorClient) {
        this.port = port;
        this.actuatorEndpoint = getBaseUri() + "actuator/";
        this.actuatorClient = actuatorClient;
        this.jarFile = jarFile;

        Assert.isTrue(jarFile.exists(), jarFile.getAbsolutePath() + " does not exist");
    }

    public void start(List<String> profiles, List<String> additionalArgs) throws InterruptedException, IOException {
        if (isUp()) {
            stop();
        }

        this.process = Runtime.getRuntime().exec(appStartCmd(jarFile, profiles, additionalArgs));

        Executors.newSingleThreadExecutor().submit(new ProcessStdOutPrinter(process));

        for (int i = 0; i < 10 && !isUp(); ++i) {
            Thread.sleep(5000);
        }
    }

    public void start(String... profiles) throws InterruptedException, IOException {
        this.start(Arrays.asList(profiles), List.of());
    }

    public void stop() throws InterruptedException {
        if (isUp()) {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            headers.setAccept(List.of(MediaType.APPLICATION_JSON_UTF8));

            actuatorClient.postForEntity(actuatorEndpoint + "shutdown", new HttpEntity<>(headers), Object.class);
            Thread.sleep(5000);
        }
        if (process != null) {
            process.destroy();
        }
    }

    private String[] appStartCmd(File jarFile, List<String> profiles, List<String> additionalArgs) {
        final List<String> cmd = new ArrayList<>(
                List.of(
                        "java",
                        "-jar",
                        jarFile.getAbsolutePath(),
                        "--server.port=" + port,
                        "--management.endpoint.heath.enabled=true",
                        "--management.endpoint.shutdown.enabled=true",
                        "--management.endpoints.web.exposure.include=*",
                        "--management.endpoints.web.base-path=/actuator"));
        if (profiles.size() > 0) {
            cmd.add("--spring.profiles.active=" + profiles.stream().collect(Collectors.joining(",")));
        }
        if (additionalArgs != null) {
            cmd.addAll(additionalArgs);
        }
        return cmd.toArray(new String[0]);
    }

    private boolean isUp() {
        try {
            final ResponseEntity<HealthResponse> response =
                    actuatorClient.getForEntity(actuatorEndpoint + "health", HealthResponse.class);
            return response.getStatusCode().is2xxSuccessful() && response.getBody().getStatus().equals("UP");
        } catch (ResourceAccessException e) {
            return false;
        }
    }

    public static Builder builder(String moduleName, String moduleVersion) {
        return new Builder(moduleName, moduleVersion);
    }

    /**
     * Configure and build a spring-boot app
     *
     * @author Ch4mp
     *
     */
    public static class Builder {

        private String moduleParentDirectory = "..";

        private final String moduleName;

        private final String moduleVersion;

        private int port = SocketUtils.findAvailableTcpPort(8080);

        private String actuatorClientId = "actuator";

        private String actuatorClientSecret = "secret";

        public Builder(String moduleName, String moduleVersion) {
            this.moduleName = moduleName;
            this.moduleVersion = moduleVersion;
        }

        public Builder moduleParentDirectory(String moduleParentDirectory) {
            this.moduleParentDirectory = moduleParentDirectory;
            return this;
        }

        public Builder port(int port) {
            this.port = port;
            return this;
        }

        public Builder actuatorClientId(String actuatorClientId) {
            this.actuatorClientId = actuatorClientId;
            return this;
        }

        public Builder actuatorClientSecret(String actuatorClientSecret) {
            this.actuatorClientSecret = actuatorClientSecret;
            return this;
        }

        /**
         * Ensures the app module is found and packaged
         * @return app ready to be started
         * @throws IOException if module packaging throws one
         * @throws InterruptedException if module packaging throws one
         */
        public ActuatorApp build() throws IOException, InterruptedException {
            final File moduleDir = new File(moduleParentDirectory, moduleName);

            packageModule(moduleDir);

            final File jarFile = new File(new File(moduleDir, "target"), moduleName + "-" + moduleVersion + ".jar");

            return new ActuatorApp(jarFile, port, new TestRestTemplate(actuatorClientId, actuatorClientSecret));
        }

        private void packageModule(File moduleDir) throws IOException, InterruptedException {
            Assert.isTrue(moduleDir.exists(), "could not find module. " + moduleDir + " does not exist.");

            String[] cmd = new File(moduleDir, "pom.xml").exists() ?
                    new String[] { "mvn", "-DskipTests=true", "package" } :
                    new String[] { "./gradlew", "bootJar" };

            Process mvnProcess = new ProcessBuilder().directory(moduleDir).command(cmd).start();
            Executors.newSingleThreadExecutor().submit(new ProcessStdOutPrinter(mvnProcess));

            Assert.isTrue(mvnProcess.waitFor() == 0, "module packaging exited with error status.");
        }
    }

    private static class ProcessStdOutPrinter implements Runnable {
        private InputStream inputStream;

        public ProcessStdOutPrinter(Process process) {
            this.inputStream = process.getInputStream();
        }

        @Override
        public void run() {
            new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(System.out::println);
        }
    }

    public String getBaseUri() {
        return "https://localhost:" + port;
    }
}