三、使用DBUnit管理数据
测试的维护一直是我比较头疼的问题,期望可以有一个比较易于维护和可复用的方法来管理这些数据。在没有更好的方法之前,暂时选用DBUnit。(反思:其实我一直在为没有发生的事情担心,使得事情根本没有进展。从已存在的、最简单的地方入手,才是正确的处理方式。)
在pom.xml中引入dbunit和springtestdbunit包,后者提供通过注解方式使用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>
<?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>这个数据文件放在了 /src/test/resources/中,与测试用例在同一个级别的目录中。为了便于区分,我采用了:Dao类名-被测试的方法名-dataset.xml 的命名方式,例如:UserDao-findByname-dataxml.set。以后如果测试用例需要修改,就可以根据名字很方便地找到对应的数据集,并且不会影响其他测试用例。
注意:
1. 这里的Element及其Attribute名称要和数据库的结构一一对应,而不是实体类。
2. 如果同一个数据对象初始化时,需要初始化的字段数目不一样,比如:一条数据需要初始化的字段是8个,而另外一个是4个。那么一定要字段数多的放在前面。
四、编写测试用例
在编写用例前,还是看下被测试的代码。用到的两个实体类:
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; } }
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); }
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 class FloorDaoTest { @Autowired private FloorDao floorDao; @Test通过代码,可以很清楚的看到通过DatabaseSetup完成了对测试数据的引入。这里在每个测试方法前引入不同的文件,如果所有的方法可以通过一个文件包括,那么也可以在类前面使用DatabaseSetup引入数据文件。@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); }}
至此,一个完整的数据层测试用例已经呈现,并且可以运行。可是实际的过程却并没有这么顺利,接下来的文章就要总结一下遇到的问题。