GithubHelp home page GithubHelp logo

contracttest's Introduction

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

contracttest's People

Contributors

ffpffcpf avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.