Spring Bootは、JUint5に基づくユニットテストを実装します

この記事では、JUnit5に基づくSpringBoot2のユニットテストの実装を紹介します。


目次

  • 前書き
  • JUnit4とJUnit5の違い
    1. テストケースの実行を無視する
    2. RunWith 構成
    3. @Before@BeforeClass@After@AfterClass置き換えられます
  • 開発環境

前書き

Spring Boot 2.2.0バージョンでは、ユニットテストのデフォルトライブラリとしてJUnit 5の導入が開始されました。SpringBoot2.2.0より前spring-boot-starter-testは、JUnit 4の依存関係が含まれいました。SpringBoot2.2.0以降、JunitJupiterに置き換えられました。


JUnit4とJUnit5の違い

1.テストケースの実行を無視する

JUnit 4:

 

@Test
@Ignore
public void testMethod() {
   // ...
}

JUnit 5:

 

@Test
@Disabled("explanation")
public void testMethod() {
   // ...
}

2.RunWith構成

JUnit 4:

 

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
    @Test
    public void contextLoads() {
    }
}

JUnit 5:

 

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ApplicationTests {
    @Test
    public void contextLoads() {
    }
}

3. @Before@BeforeClass@After@AfterClass交換されます

  • @BeforeEach 交換 @Before
  • @BeforeAll 交換 @BeforeClass
  • @AfterEach 交換 @After
  • @AfterAll 交換 @AfterClass

開発環境

  • JDK 8

  1. SpringBootプロジェクトを作成します。SpringBootプロジェクトを作成するにはIntelliJIDEAを参照してください

  2. spring-boot-starter-web依存関係を追加すると、最終的にはpom.xml次のようになります。

 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>tutorial.spring.boot</groupId>
    <artifactId>spring-boot-junit5</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-junit5</name>
    <description>Demo project for Spring Boot Unit Test with JUnit 5</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. プロジェクトが作成されると、テストクラスが自動的に生成されます。

 

package tutorial.spring.boot.junit5;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootJunit5ApplicationTests {

    @Test
    void contextLoads() {
    }

}

このテストクラスの機能は、アプリケーションコンテキストを正常に開始できるかどうかを確認することです。@SpringBootTest注釈は、Spring Bootに@SpringBootApplicationメインの構成クラスノートを検索し、そのクラスを使用してSpringアプリケーションコンテキストを開始するように指示します。

  1. テストするアプリケーションロジックコードを補足する

4.1。サービスレイヤーインターフェイスを定義します

 

package tutorial.spring.boot.junit5.service;

public interface HelloService {

    String hello(String name);
}

4.2。コントローラーレイヤーを定義する

 

package tutorial.spring.boot.junit5.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import tutorial.spring.boot.junit5.service.HelloService;

@RestController
public class HelloController {

    private final HelloService helloService;

    public HelloController(HelloService helloService) {
        this.helloService = helloService;
    }

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable("name") String name) {
        return helloService.hello(name);
    }
}

4.3。サービスレイヤーの実装を定義する

 

package tutorial.spring.boot.junit5.service.impl;

import org.springframework.stereotype.Service;
import tutorial.spring.boot.junit5.service.HelloService;

@Service
public class HelloServiceImpl implements HelloService {

    @Override
    public String hello(String name) {
        return "Hello, " + name;
    }
}
  1. HTTPリクエストを送信するユニットテストを作成します。

 

package tutorial.spring.boot.junit5;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {

    @LocalServerPort
    private int port;

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void testHello() {
        String requestResult = this.restTemplate.getForObject("http://127.0.0.1:" + port + "/hello/spring",
                String.class);
        Assertions.assertThat(requestResult).contains("Hello, spring");
    }
}

説明:

  • webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT ローカルランダムポートを使用してサービスを開始します。
  • @LocalServerPort同等@Value("${local.server.port}");
  • webEnvironmentの構成では、Spring Bootが自動的にTestRestTemplate例を提供し、HTTP要求を送信するために使用できます。
  • TestRestTemplateHTTP要求の例を除いて、手段はorg.springframework.test.web.servlet.MockMvc次のように同様の機能を実行することもできます。

 

package tutorial.spring.boot.junit5.controller;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

    @Autowired
    private HelloController helloController;

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testNotNull() {
        Assertions.assertThat(helloController).isNotNull();
    }

    @Test
    public void testHello() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("Hello, spring"));
    }
}

上記のテスト方法は、全体的なテストに属します。つまり、すべてのアプリケーションコンテキストがアクティブ化されます。また、コントローラー層のみをテストするなど、階層化されたテスト方法もあります。

  1. 階層化されたテスト。

 

package tutorial.spring.boot.junit5.controller;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import tutorial.spring.boot.junit5.service.HelloService;

@WebMvcTest
public class HelloControllerTest {

    @Autowired
    private HelloController helloController;

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private HelloService helloService;

    @Test
    public void testNotNull() {
        Assertions.assertThat(helloController).isNotNull();
    }

    @Test
    public void testHello() throws Exception {
        Mockito.when(helloService.hello(Mockito.anyString())).thenReturn("Mock hello");
        this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.content().string("Mock hello"));
    }
}

説明:

  • @WebMvcTest注は、全体的なコンテキストをインスタンス化するのではなく、Spring Bootにインスタンス化されたControllerレイヤーのみを指示し、さらに、Controllerレイヤーのインスタンスのみをインスタンス化するように指定します@WebMvcTest(HelloController.class)
  • コントローラ層の唯一の例であるため、依存するサービス層インスタンスを@MockBean作成する必要がありMockito指定さたメソッドによって、特定の状況でサービス層インスタンスメソッドのモックアウトが呼び出されたときに結果が返されます。

おすすめ

転載: blog.csdn.net/suifeng629/article/details/107120186