既然我们要使用到spring测试的模块那我们肯定就需要到pom文件里面导入一个spring测试的jar包,那么至于详细导那个包,还得看你的junit的版本号,要与junit的版本号相近的版本号的包,如下:
在pom.xml文件里面编写
junit的jar包的路径
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
spring-test的jar包的路径
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.13.RELEASE</version>
<scope>test</scope>
</dependency>
导入包之后我们就要开始编写spring测试模块了,那么测试类放在哪儿呢:如图所示:
代码如下:
TestMapper.java
package com.anzhuo.cm.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.anzhuo.cm.dao.SaattendanceMapper;
import com.anzhuo.cm.pojo.Saattendance;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class TestMapper {
@Autowired
SaattendanceMapper sm;
@Test
public void test(){
Saattendance ss = sm.selectByPrimaryKey(1);
System.out.println(ss.getsNumber());
System.out.println(ss.getSaTruant());
}
}
这样就可以测试啦,能查询出来的话,说明成功