Skip to content

ffpffcpf/ContractTest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ContractTest

use Moscow and spring-test-dbunit to make contract test easily

Help you use this Step by Step!

1.Dependencies Information


<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.

2.Use Configuration Class to extend ContractTest

public TestBaseBean extends ContractTest{  
...  
}

3.Use @ContextConfiguration to register bean in context

@ContextConfiguration(location={"spring.xml","spring-servlet.xml","dataSource.xml"})
public TestBaseBean extends ContractTest{  
 ...  
}

4.write the test method which name is contract description

@ContextConfiguration(location={"spring.xml","spring-servlet.xml","dataSource.xml"})
public TestBaseBean extends ContractTest{  
   @Test  
   public void should_return_hello_world(){  
    assertContract();  
  }  
}

Now you finish configuration in java code, then you need to create your contract.

1.Create Contract

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.

3.Create dataSet in the same package as the test class under src/test/resources

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

About

use Moscow to make contract test easily

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages