在非maven中只要加载mybaits就好,但是在maven项目中还需要加载spring的配置文件,否则找不到bean import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframew
import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 如果要在maven 中使用junit ,除了需要加载mybatis的配置文件,还有加载spring的配置文件,否则找不到bean,这个bug找了一天 * @author Administrator * */ @ContextConfiguration(locations = { "classpath:spring-mybatis.xml","classpath:spring-mvc.xml","classpath:mybatis-config.xml","classpath:spring-service.xml" }) @RunWith(SpringJUnit4ClassRunner.class) public abstract class TestBefore extends AbstractJUnit4SpringContextTests { }使用 ContextConfiguration 注释加载
import java.util.List; import javax.annotation.Resource; import org.junit.Before; import org.junit.Test; import com.hrms.pojo.Admin; import com.hrms.service.impl.AdminServiceImpl; //import com.github.pagehelper.PageInfo; public class TestUser extends TestBefore { @Resource AdminServiceImpl adminServiceImpl; @Before public void setUp() throws Exception { System.out.println("Dd"); } @Test public void test() { System.out.println("Ddd"); List ddd=adminServiceImpl.getAll(); System.out.println(ddd.size()); } // @Test // public void queryByPageTest(){ // PageInfopage = userServiceImpl.queryByPage(null, 1, 6); // System.out.println(page); // for(int i=0;i