前言:一直以來本身寫的程序都是網絡上已經搭建好的了,本身只要按照裏面的例子來進行就可以快速掌握了,對於裏面的不少細節本身也沒有在乎,也沒有時間去花那個功夫研究下,今天想在一個糟糕的二期項目中進行junit的測試,而後不少的細節問題就來了。
java
1,在maven中添加junti的jarspring
<!-- Test --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency>
2,建立程序包json
3,編寫類
tomcat
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;mybatis
4,解決辦法app
這樣的話,咱們就要進行配置,如圖:test目錄進行該,src目錄也相同。我就不截圖了maven
ok,在運行看看,沒有問題啦!測試