2017-07-06 1 views
1

スプリングブートバージョン1.5.3.RELEASE;なぜH2によるSpringブートテストですか?しかし、ローカルのmysql dbにデータを書き込みますか?

application-test.yml ファイルの場所:../src/test/resources

spring: 
    datasource: 
    driver-class-name: org.h2.Driver 
    url: jdbc:h2:mem:test;MODE=MySQL 
    profiles: 
    active: test 

と私のテストBaseClassのは、次のとおりです。

@RunWith(SpringRunner.class) 
@SpringBootTest(
     classes = RestApiConfiguration.class, 
     webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT 
) 
@ActiveProfiles("test") 
public class BaseRestApiTest { 

    @Autowired 
    protected TestRestTemplate restTemplate; 

} 

、今BaseRestApiTestを拡張するテストクラスでは、私はhttp://127.0.0.1:8080/api/user/createのようなURLにデータを投稿すると、最後に、ユーザデータがローカルのMySQL DBに書き込まれました。期待

@Test 
    public void testAdd() { 
     String url = HOST + "/api/user/create"; 
     Map<String, Object> form = Maps.newHashMap(); 
     form.put("userId", 1); 

     MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(form.size()); 
     params.setAll(form); 

     HttpHeaders httpHeaders = new HttpHeaders(); 
     httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); 

     HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(params, httpHeaders); 
     ResponseEntity<Long> resp = restTemplate.exchange(url, HttpMethod.POST, entity, Long.class); 
     Assert.assertNotNull(resp.getBody()); 
    } 

は、ユーザデータがメモリだけでH2に書かれたが、MySQL DB私の地元はなかったです。

あなたBaseRestApiTestはすでにので、あなたのアプリケーションtest.ymlに持っている必要がありますすべては以下の通りです@ActiveProfiles(「テスト」)でマークされ

答えて

1

:このサンプルのように、

spring: 
jpa: 
    database: HSQL 

と依存の、それはライン追従しているので、私は、Gradleの使用:あなたはMavenを使用している場合

testCompile group: 'org.hsqldb', name: 'hsqldb' 

OFCを、あなたのpom.xmlファイルにMavenの形式で記述する必要があります。

関連する問題