1.在IDEA指定目录为test的目录
2.在pom文件引入相应的junit包,并指定为test单元测试才有效编译
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
3.新建一个类,引入相应的spring配置,并引入@Test即可单元业务逻辑的测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
public class XXXMTest {
//引用的你service类 dao类的服务层 or 数据持久层
}
4.@Transational注解加上,在单元测试时,会执行相应的sql的CURD操作,然后会进行回滚,也就是没有对库的任何实际性操作(这个功能很好,方便测试,不影响的实际数据内容)
@Test
@Transactional
public void insertSelective() {
//你的业务逻辑...
}