前言:一直以来自己写的程序都是网络上已经搭建好的了,自己只要按照里面的例子来进行就能够快速掌握了,对于里面的很多细节自己也没有在意,也没有时间去花那个功夫研究下,今天想在一个糟糕的二期项目中进行junit的测试,然后很多的细节问题就来了。
1,在maven中添加junti的jar
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
2,创建程序包
3,编写类
package wx;
import net.sf.json.JSONArray;
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.zp.mapper.WxJoinMapper;
import com.zp.model.WxJoin;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config/spring-common.xml",
"classpath:config/mybatis-config.xml" })
public class WxJoinTest {
@Autowired
WxJoinMapper joinMapper;
@Test
public void save() {
if (joinMapper == null) {
System.out.println("空");
}
WxJoin findById = joinMapper.findById(1);
JSONArray fromObject = JSONArray.fromObject(findById);
System.out.println(fromObject.toString());
}
}
如果这个时候运行的话,会报:Class not found wx.WxJoinTest
为什么呢,我指定了默认编译的路径是:zpwxms/WebRoot/WEB-INF/classes,之所以指定是因为发布到tomcat的时候,zpwxms/WebRoot/WEB-INF/classes没有编译的类和相关的jar;而junit的类路径为:target/classes;
4,解决办法
这样的话,我们就要进行配置,如图:test目录进行该,src目录也相同。我就不截图了
ok,在运行看看,没有问题啦!