spring 下测试代码编写
3. 测试代码的重用
测试开始前需要模拟比较多的数据,测试完成后又需要清除这些模拟数据,测试service时需要,测试controller时也需要,所以一般都会把模拟测试数据的工具类放在service层的测试代码中,同时测试controller时引用之。maven配置中需要添加plugin编译并生成测试代码,同时在controller中添加对service包测试代码的引用。
service包中添加plugin配置以生成test的jar:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
controller中添加对test的jar的依赖:
<!-- for Test -->
<dependency>
<groupId>com.dragonwell</groupId>
<artifactId>dragonwell-etlbusiness</artifactId>
<version>1.0</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>