やりたいこと
GET /prometheus
を叩いてHttpStatus200を確認するテストを実装したい。
Spring Bootのバージョンアップ等でうっかり消えてしまっていたというミスを無くすため。
問題
実際にアプリケーションを起動すると叩けるのに、テストだと404になってしまう。。。
GET /info, GET /healthは200なのに
問題のコード
テストフレームワークはSpockを使用しています。
application.yml
management:
endpoints:
web:
base-path: /
exposure:
include: info, health, prometheus
PrometheusSpec.groovy
@SpringBootTest
@AutoConfigureMockMvc
class PrometheusSpec extends Specification {
@Autowired
MockMvc mockMvc
def "GET /prometheus"() {
when:
ResultActions actual = mockMvc.perform(MockMvcRequestBuilders
.get("/prometheus"))
then:
actual.andReturn().getResponse().getStatus() == HttpStatus.OK.value()
}
}
実行結果
actual.andReturn().getResponse().getStatus() == HttpStatus.OK.value()
| | | | | | | |
| | | 404 | | | 200
解決方法
@AutoConfigureMetrics
をclassに付与することで解決。
クラスパスに関係なく、@SpringBootTest を使用する場合、メモリ内でバックアップされているものを除いて、メーターレジストリは自動構成されません。
統合テストの一環としてメトリックを別のバックエンドにエクスポートする必要がある場合は、@AutoConfigureMetrics でアノテーションを付けます。
あとがき
ドキュメント読むの大事。