banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

SpringBoot中使用JUnit进行代码测试

title: SpringBoot 中使用 JUnit 进行代码测试
date: 2021-08-24 15:30:00
toc: true
category:

  • Java
    tags:
  • Java
  • SpringBoot
  • 单元
  • 测试

Junit は Java で有名なテストフレームワークであり、Junit + SpringBoot Test を統合することで、テストプログラムで SpringBoot の依存性注入などの SpringBoot の機能を使用することができます。

依存関係#

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>

テスト Service#

位置:src/main/java/com/example/service/impl/HelloServiceImpl.java

package com.example.service.impl;

@Service
public class HelloServiceImpl implements HelloService {

    @Override
    public String helloTo(String name) {
        // 故意にバグを書く
        if ("C++".equals(name))
            return "Don't hello to C++";
        return StringUtils.hasText(name) ? "HELLO " + name : "HELLO WORLD";
    }
}

このコードをテストするために、Java の一般的な方法は、必要なリソースを自分でnewしてからテストに使用することですが、これでは SpringBoot のさまざまな機能を使用することができません。必要な依存関係が非常に多い場合や、様々な設定の読み込みが関わる場合は、まさに悪夢です。

private HelloService helloService;

@Before
public void setUp() {
    helloService = new HelloService();
}

@Test
public void helloTo() {
    assertEquals("HELLO WORLD", helloService.helloTo(""));
    assertEquals("HELLO WORLD", helloService.helloTo("WORLD"));
    assertEquals("HELLO JAVA", helloService.helloTo("JAVA"));
    assertEquals("HELLO C++", helloService.helloTo("C++")); // エラー
}

方法 1:アノテーションで SpringBoot を起動する#

位置:src/test/java/com/example/service/impl/HelloServiceImplTest.java

重要なポイント:

  • Junit SpringBoot Test フレームワークを使用する:@RunWith(SpringRunner.class)
  • 起動エントリポイントを指定する:@SpringBootTest(classes = [SpringBootのプログラムエントリクラス])

テストが終了すると、SpringBoot コンテキストは自動的に閉じられます。

package com.example.service.impl;

import com.example.SpringBootTestApplication;
import com.example.service.HelloService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootTestApplication.class)
public class HelloServiceImplTest {

    @Autowired
    private HelloService helloService;

    @Test
    public void helloTo() {
        assertEquals("HELLO WORLD", helloService.helloTo(""));
        assertEquals("HELLO WORLD", helloService.helloTo("WORLD"));
        assertEquals("HELLO JAVA", helloService.helloTo("JAVA"));
        assertEquals("HELLO C++", helloService.helloTo("C++")); // エラー
    }
}

方法 2:明示的に SpringBoot を起動する#

SpringApplication::runメソッドを使用して SpringBoot プログラムを起動することで、SpringBoot のすべての機能を使用してテストを行うこともできますが、少し手間がかかりますし、テストが終了した後にSpringBootContextを手動で閉じる必要があります。

package com.example.service.impl;

import com.example.SpringBootTestApplication;
import com.example.service.HelloService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;

import static org.junit.Assert.assertEquals;

public class HelloServiceImplTest2 {

    private ConfigurableApplicationContext context;
    private HelloService helloService;

    @Before
    public void setUp() {
        context = SpringApplication.run(SpringBootTestApplication.class);
        // Beanを明示的に取得し、必要な依存関係をSpringBootが自動的に注入します
        helloService = context.getBean(HelloService.class);
    }

    @After
    public void tearDown() {
        // SpringBootプログラムを閉じる
        context.close();
    }

    @Test
    public void helloTo() {
        assertEquals("HELLO WORLD", helloService.helloTo(""));
        assertEquals("HELLO WORLD", helloService.helloTo("WORLD"));
        assertEquals("HELLO JAVA", helloService.helloTo("JAVA"));
        assertEquals("HELLO C++", helloService.helloTo("C++")); // エラー
    }
}
読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。