The path to continuous integration-unit testing of the data Access Layer (cont.)

Source: Internet
Author: User
Tags assert
In the previous article, the configuration of the data source for testing was completed. The following continues to build the tests that can be run.

Iii. using Dbunit to manage data

The maintenance of the test has always been a headache for me, and I expect to have a more maintainable and reusable way to manage this data. In the absence of a better method, temporarily choose Dbunit. ( reflection: I've been worrying about things that didn't happen, so things didn't make any progress at all.) From the existing, the simplest place to start, is the right way to handle. )

The Dbunit and Springtestdbunit packages are introduced in Pom.xml, which provides an annotated way to use the Dbunit:

        <dependency>
            <groupId>org.dbunit</groupId>
            <artifactid>dbunit</artifactid >
            <version>2.4.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.springtestdbunit</groupId>
            <artifactId> spring-test-dbunit</artifactid>
            <version>1.0.1</version>
            <scope>test</ Scope>
        </dependency>

Dbunit uses XML files to manage datasets, and it is also easy to support JSON formats by using third-party libraries. XML is used here:

<?xml version= "1.0" encoding= "Utf-8"?>
<dataset>
    <building id= "1" name= "SOHO"/>
    < Building id= "2" name= "New Gate Plaza"/>
    <floor id= "1" floor_num= "2" building= "1"/>
    <floor id= "2" floor_num= "3" building= "1"/>
    <floor id= "3" floor_num= "5" building= "2"/>
</dataset>
This data file is placed in the/src/test/resources/, in the same level of directory as the test case. For the sake of distinction, I used: DAO class name-the name of the method name-dataset.xml to be tested, for example: Userdao-findbyname-dataxml.set. Later, if the test case needs to be modified, it is easy to find the corresponding data set by name and does not affect other test cases.

Attention:

1. The element and its attribute name here correspond to the structure one by one of the database, not the entity class.

2. If the same data object is initialized, the number of fields that need to be initialized is different, for example: one data needs to initialize 8 fields, and the other one is 4. Then be sure to put more fields in front of the number.

Iv. Writing test Cases

Before you write a use case, look at the code that is being tested. The two entity classes used:

Package com.noyaxe.myapp.entity;

Import javax.persistence.Entity;
Import javax.persistence.Table;

@Entity
@Table (name = "Building") public
class Building extends IdEntity {
    private String name;


    Public String GetName () {
        return name;
    }

    public void SetName (String name) {
        this.name = name;
    }
}

Package com.noyaxe.myapp.entity;

Import Javax.persistence.Column;
Import javax.persistence.Entity;
Import Javax.persistence.JoinColumn;
Import Javax.persistence.ManyToOne;
Import javax.persistence.Table;

@Entity
@Table (name = "Floor") public
class Floor extends IdEntity {
    private Integer floornum;
    Private Building Building;

    @Column (name = "Floor_num") public
    Integer Getfloornum () {
        return floornum;
    }

    public void Setfloornum (Integer floornum) {
        this.floornum = floornum;
    }

    @ManyToOne (optional = False)
    @JoinColumn (name = "Building") public
    Building Getbuilding () {
        return building;
    }

    public void setbuilding (Building Building) {
        this.building = Building;
    }
}

Tested Floordao:

Package com.noyaxe.myapp.repository;

Import Com.noyaxe.myapp.entity.Floor;
Import Org.springframework.data.jpa.repository.JpaSpecificationExecutor;
Import org.springframework.data.repository.PagingAndSortingRepository;

Import java.util.List;

Public interface Floordao extends Jpaspecificationexecutor<floor>, Pagingandsortingrepository<floor, Long > {Public Floor
    findbybuildingnameandfloornum (String Building, Integer floornum);

    Public list<floor> Findbybuildingname (String Building);
}

The test case is also very simple:

Package com.noyaxe.myapp.repository;
Import Com.github.springtestdbunit.annotation.DatabaseSetup;
Import Com.noyaxe.myapp.entity.Floor;
Import Org.junit.Test;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import Org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

Import Org.springframework.test.context.support.DirtiesContextTestExecutionListener;

Import java.util.List;
Import static junit.framework.Assert.assertNull;
Import static org.junit.Assert.assertEquals;

Import static org.junit.Assert.assertNotNull; @RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationcontext-test.xml") @ Testexecutionlisteners ({        Dependencyinjectiontestexecutionlistener.class,         Dirtiescontexttestexecutionlistener.class,         Transactiondbunittestexecutionlistener.class}) public classfloordaotest {@Autowired private Floordao Floordao;
 @Test
    @DatabaseSetup ("Floordao-findbbybuidlingname-dataset.xml")
public void Testfindbybuildingname () {list<floor> singlefloorlist = floordao.findbybuildingname ("SOHO"); Assertequals (1, singlefloorlist.size ()); list<floor> twofloorlist = Floordao.findbybuildingname ("New Gate Plaza"); Assertequals (2, Twofloorlist.size ()); list<floor> emptyfloorlist = Floordao.findbybuildingname ("Test"); Assertequals (0, Emptyfloorlist.size ()); } @Test
    @DatabaseSetup ("Floordao-findbbybuidlingnameandfloornum-dataset.xml")
public void Testfindbybuildingnameandfloornum () {Floor floor = floordao.findbybuildingnameandfloornum ("SOHO", 2); Assertnotnull (floor); Floor empty = Floordao.findbybuildingnameandfloornum ("New Gate Plaza", 7); Assertnull (empty); Empty = Floordao.findbybuildingnameandfloornum ("No Building", 7); Assertnull (empty); }} through the code, it is clear to see that the introduction of the test data has been completed through Databasesetup. This introduces different files before each test method, and if all the methods can be included with a file, you can also use Databasesetup to introduce the data file before the class.

At this point, a complete data-tier test case has been presented and can be run. But the actual process is not so smooth, the next article will summarize the problems encountered.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.