use Moscow and spring-test-dbunit to make contract test easily
<dependency>
<groupId>com.github.macdao</groupId>
<artifactId>moscow</artifactId>
<version>0.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.2</version>
<scope>test</scope>
</dependency>
Also,spring-framework is necessary.
public TestBaseBean extends ContractTest{
...
}
@ContextConfiguration(location={"spring.xml","spring-servlet.xml","dataSource.xml"})
public TestBaseBean extends ContractTest{
...
}
@ContextConfiguration(location={"spring.xml","spring-servlet.xml","dataSource.xml"})
public TestBaseBean extends ContractTest{
@Test
public void should_return_hello_world(){
assertContract();
}
}
Firstly, I'm show you an example below:
[
{
"description": "should_return_hello_world",
"request": {
"uri": "/sayHello",
"method": "post",
"json": {
"name": "Tom",
"sex" : "Man"
}
},
"response": {
"status": 200,
"json": {
"name": "Tom",
"sex": "Man",
"words": "hello world"
}
}
}
]
The controller code:
@RequestMapping(value = "sayHello",method = RequestMethod.POST
@ResponseBody
public Object sayHello(@RequestBody Person person){
String words = wordsService.getWordsByName(person.getName());//get words from database
person.setWords(words);
return person;
}
The Person code:
public class Person{
private String name;
private String sex;
private String words;
//getter and setter
...
}
In the contract, request object describe what and where you send to the server, response object describe what you expect from server response.
The contract use Moco to describe, you can see the detail in Moco.
Importantly, you should put your contracts in src/test/resources/contracts.
In order to setup and teardown database tables using simple annotations as well as checking expected tables contents once a test completes, I use spring test dbunit.
1.Because of some reason, you should remove all bean which name is datasource before using.(If there is not datasource bean, skip this step).
2.Put init SQL scripts in src/test/resources/database/h2, so that the embedded H2 database create table and ready to be used.
Here is a dataset example:
<dataset>
<person name="Tom" sex="Man" words="hello world" />
</dataset>
4.use @DatabaseSetup and @DatabaseTearDown to describe how the tables like before and after testing.
@Test
@DatabaseSetup("data_insert.xml")
@DataTearDown("data_delete.xml")
public void should_return_hello_world(){
assertContract();
}
The spring test dbunit can also check expected tables contents once test completes, config multiples datasource and write customer loader(default loader is xml loader).You can see more detail in spring test dbunit