0
0

(調査中)Spring MVC コンストラクタ内で例外が発生する状況のモック化

Posted at

Mockitoを使用してコンストラクタ内で例外が発生する状況をテストすることができます。Mockito-inlineを利用することで、コンストラクタ内で特定の依存関係が例外をスローするようにモックし、その結果としてコンストラクタが適切に例外を処理するかどうかをテストできます。

以下に、例外処理をテストするための具体的な例を示します。

例: コンストラクタ内で例外が発生する状況のモック


コントローラクラス

まず、例外処理を含むコントローラクラスを定義します。

import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;

@Controller
public class MyController {

    private final MyService myService;
    private String message;

    @Autowired
    public MyController(MyService myService) {
        this.myService = myService;
        try {
            this.message = myService.greet();
        } catch (Exception e) {
            this.message = "Error occurred";
        }
    }

    public String getMessage() {
        return message;
    }
}

サービスクラス

次に、サービスクラスを定義します。

public class MyService {
    public String greet() throws Exception {
        return "Hello, World!";
    }
}


テストクラス

最後に、例外処理をテストするためのテストクラスを作成します。

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MyControllerTest {

    @Mock
    private MyService myService;

    @InjectMocks
    private MyController myController;

    @BeforeEach
    public void setUp() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    public void testConstructorExceptionHandling() throws Exception {
        // モックの振る舞いを定義して例外をスローさせる
        when(myService.greet()).thenThrow(new Exception("Service error"));

        // コントローラのインスタンスを作成
        myController = new MyController(myService);

        // コンストラクタが例外を適切に処理しているかをテスト
        assertEquals("Error occurred", myController.getMessage());
    }
}

実行結果

(編集中)


説明

依存関係の追加:

Mockito-inlineやSpring Testの依存関係は、前述の通りです。

コントローラの定義:

MyControllerはコンストラクタでMyServiceを受け取り、そのgreetメソッドを呼び出します。greetメソッドで例外がスローされた場合、クラス変数messageに"Error occurred"が設定されるようにしています。

サービスの定義:

MyServiceはgreetメソッドを持ち、例外をスローする可能性があります。

テストクラスの作成:

MyControllerTestでMockitoのアノテーション@Mock@InjectMocksを使用して、サービスとコントローラのインスタンスを作成します。

@BeforeEachメソッドでMockitoAnnotations.openMocks(this);を使用してモックを初期化します。

when(myService.greet()).thenThrow(new Exception("Service error"));でモックの振る舞いを定義し、greetメソッドが呼ばれた際に例外をスローするようにします。

myController = new MyController(myService);でコントローラのインスタンスを作成し、コンストラクタが例外を適切に処理しているかを確認します。

assertEquals("Error occurred", myController.getMessage());で、クラス変数messageが正しく設定されているかをテストします。

これにより、コンストラクタ内で発生する可能性のある例外処理の動作を確認することができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0