今日もBuilding an Application with Spring Bootの続きを読む。今日は単体テストのところあたりから。
単体テスト
まずは単体テストを行う。
build.gradleのdependenciesブロックに以下を追加する。testCompileにテスト用のライブラリを渡すことで、テスト時にこれが使えるようになる。
testCompile("org.springframework.boot:spring-boot-starter-test:1.2.1.RELEASE")
そして、次のコードがテストコード。
package sample;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class HelloControllerTest {
private MockMvc mvc;
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}
@Test
public void getHello() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("Hello World!")));
}
}
@SpringApplicationConfigurationに渡しているMockServletContextが空のWebApplicationContextを作り、@BeforeのsetUpメソッドでHelloControllerをMockMvcとして生成することでテストが可能となっている。あとはMockMvcRequestBuildersで擬似的にアクセスしてテスト結果を照合すればOK。しかし、アノテーションが多い。流儀に従えばいいだけなんだろうけれど、サラサラと出てくるもんだろうか?
インテグレーションテスト
さきほどの単体テストではMockに置き換えていたが、インテグレーションテストも比較的簡単に行えるようだ。
package sample;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate;
import java.net.URL;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest({"server.port=0"})
public class HelloControllerIT {
@Value("${local.server.port}")
private int port;
private URL base;
private RestTemplate template;
@Before
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
this.template = new TestRestTemplate();
}
@Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
assertThat(response.getBody(), equalTo("Hello World!"));
}
}
@IntegrationTestアノテーションでポートを指定しておいて、privateなメンバ変数portに@Valueアノテーションで渡すとよい、と理解した。
この例はシンプルなものだったので、簡単そうに見えた。
Production Gradeのサービスを追加する
ウェブサイトを作ったら監視は付き物。Spring Bootはいくつかの監視用モジュールが準備されている。
build.gradleのdependenciesブロックに以下を追加する。
compile("org.springframework.boot:spring-boot-starter-actuator:1.2.1.RELEASE")
そしてSpring Bootを再ビルドしたあと、起動すると、様々な状態監視用のルートがマッピングされている。
エンドポイントの詳細についてはEndpointsを参照するといい。
設定は、application.propertiesファイルに記述することで、できるようである。
あとはSpring Boot Starterを見よ
そして、全てのサンプルソースコードが見られるようだ。
Spring BootはGroovyもサポートしてるよ
Groovyでも書けるらしい。あとは実行可能なjarにまとめることができると。これは最初のほうに書いてあったような…。
読み終わったよ!
あとはSpring Bootのオンラインドキュメントを読みながら深く掘り下げてみよう!だそうである。
そうしよう。とりあえず今日はここまで。
