Spring Boot + Kotlinでmockitoを使ってモック化したテストコードを書く


今回はkotlinでmockito使って処理をモック化したテストコードを書いてみた。Mockito framework site

セットアップ

1
2
3
4
5
6
7
dependencies {
   ...

    testCompile "org.springframework.boot:spring-boot-starter-test"
    testCompile "org.mockito:mockito-core:$mockito_core_version"
    testCompile 'junit:junit:$junit_version'
}

※ mockitoは1.10.19を使いました。

モック化するクラス

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
interface ElasticsearchClient {
    fun foo() : String
}

@Service
class ElasticsearchClientImpl : ElasticsearchClient {
    override fun foo() : String {
        return "foo"
    }
}

テスト対象のコントローラー

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@RestController
@EnableAutoConfiguration
@ComponentScan
@Api(description = "ヘルスチェック")
open class HealthCheckController constructor(val elasticsearchClient: ElasticsearchClient) {

    @ApiModel
    data class HealthCheck(
            @ApiModelProperty(required = true, value = "結果", example = "true")
            val result: Boolean,
            @ApiModelProperty(required = true, value = "ステータス", example = "OK")
            val status: String
    )

    @ApiOperation(value = "ヘルスチェック")
    @RequestMapping(value = "/health-check", method = arrayOf(RequestMethod.GET))
    fun index(): Response<HealthCheck> {
        return Response(HealthCheck(true, "OK"))
    }

    @ApiOperation(value = "ヘルスチェック")
    @RequestMapping(value = "/health-check/elasticsearch", method = arrayOf(RequestMethod.GET))
    fun elasticsearch(): Response<HealthCheck> {
        return Response(HealthCheck(true, elasticsearchClient.foo()))
    }
}

テストコード

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@RunWith(SpringJUnit4ClassRunner::class)
@ContextConfiguration(classes = arrayOf(Application::class))
@WebAppConfiguration
class HealthCheckControllerTest {

    lateinit var mvc: MockMvc
    lateinit var elasticsearchClient: ElasticsearchClient
    lateinit var target: HealthCheckController

    @Before
    fun setup() {
        elasticsearchClient = Mockito.mock(ElasticsearchClient::class.java)
        target = HealthCheckController(elasticsearchClient)

        MockitoAnnotations.initMocks(this)
        mvc = MockMvcBuilders.standaloneSetup(target).build()
    }

    @Test
    fun testElasticsearch() {
        val type = Types.newParameterizedType(Response::class.java, HealthCheckController.HealthCheck::class.java)
        val adapter: JsonAdapter<Response<HealthCheckController.HealthCheck>> = Moshi.Builder().build().adapter(type)

        Mockito.`when`(elasticsearchClient.foo()).thenReturn("NG")

        val result: MvcResult = mvc.perform(get("/health-check/elasticsearch"))
                .andExpect(status().isOk()).andReturn()
        val response = adapter.fromJson(result.response.contentAsString)

        assertThat(response.value.result, Is.`is`(true))
        assertThat(response.value.status, Is.`is`("NG"))
    }
}
setup()メソッド
1
2
3
4
5
6
7
8
    @Before
    fun setup() {
        elasticsearchClient = Mockito.mock(ElasticsearchClient::class.java)
        target = HealthCheckController(elasticsearchClient)

        MockitoAnnotations.initMocks(this)
        mvc = MockMvcBuilders.standaloneSetup(target).build()
    }
モック化はどこで?
1
Mockito.`when`(elasticsearchClient.foo()).thenReturn("NG")
1
2
assertThat(response.value.result, Is.`is`(true))
assertThat(response.value.status, Is.`is`("NG"))