11

Spring Boot および RESTful サービスの操作に関する多くのガイドを読みましたが、それらの多くには単体テストの実行に関する情報が含まれており、特に「Spring Boot を使用したアプリケーションの構築」が有名です。ただし、クラウド マイクロサービス アーキテクチャで一般的であるように、他の Spring Boot アプリケーションを消費/依存する Spring Boot アプリケーションを単体テストする方法の例を示すものは見たことがありません。たとえば、次の Spring Boot サービスがあります。

ServiceMediator、Adapter1、Adapter2

ServiceMediator は、入力に応じて Adapter1 または Adapter2 を呼び出します。

Spring JUnit テストで ServiceMediator を開始してテストする前に、Spring Boot サービス Adapter1 および Adapter2 を開始する方法はありますか?

4

2 に答える 2

7

process-exec-maven-pluginは、統合テスト前のフェーズで (標準の Spring Boot アプリとして)複数の Java プロセスを開始できるようにするのに役立ち、統合テスト後のフェーズでそれらを自動的にシャットダウンします。.

注:統合テストは、 maven-failsafe-pluginをspring-boot-maven-pluginで構成する必要があるため、統合テストフェーズで実行する必要があります。次に、統合テストを実行するには、verify以上の maven Lifecycle をターゲットにする必要があります。これは、統合テストフェーズが実際には packageverify Lifecyclesの間にあるためです。 Default Lifecycles を参照してください。

次のmaven(pom.xml)構成がうまくいきました:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.3.5.RELEASE</version>
            <executions>                    
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <skip>${integration-tests.skip}</skip>
                    </configuration>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                    <configuration>
                        <skip>${integration-tests.skip}</skip>
                    </configuration>
                </execution>
            </executions>
        </plugin>           

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <skip>${integration-tests.skip}</skip>                  
                <includes>
                    <include>**/*IT.java</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>                        
                </execution>                    
            </executions>
        </plugin>

        <plugin>
            <groupId>com.bazaarvoice.maven.plugins</groupId>
            <artifactId>process-exec-maven-plugin</artifactId>
            <version>0.7</version>
            <executions>                    
                <execution>
                    <id>switchboard-process</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>start</goal>
                    </goals>
                    <configuration>
                        <name>accounts-service</name>
                        <workingDir>/../../micro-service</workingDir>
                        <waitForInterrupt>false</waitForInterrupt>                          
                        <arguments>
                            <argument>java</argument>
                            <argument>-jar</argument>
                            <argument>${basedir}/../micro-service/target/micro-service-${project.version}-exec.jar</argument>
                        </arguments>
                    </configuration>
                </execution>
                <!--Stop all processes in reverse order-->
                <execution>
                    <id>stop-all</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop-all</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

test.javaフォルダーにIntegration Test クラス ( WebServerIT ) がある場合:

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = WebServerApp.class)
@WebIntegrationTest("server.port:0")
public class WebServerIT {

    @Autowired
    private WebApplicationContext webServerAppContext;

    private MockMvc webServerMockMvc;

    @Before
    public void setUp() {
        System.out.println("the test is set up");
        webServerMockMvc = MockMvcBuilders.webAppContextSetup(webServerAppContext).build();
    }

    /**
     * This test calls the WebServer's endpoint (/accounts/123456789) which in turn calls the micro-service rest api 
     * which is started using the process-exec-maven-plugin, otherwise the test would fail.
     */
    @Test
    public void testWebServerInteractionWithMicroService() throws Exception {
        this.webServerMockMvc.perform(get("/accounts/123456789"))
                .andExpect(status().isOk());
    }
}
于 2016-05-23T18:12:34.333 に答える