当我运行此代码时
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration(locations={"classpath:spring/applicationContext*.xml"}) //加载配置文件
public class JunitTest {
@Autowired
private ProjectDao prjDao;
@BeforeClass
public static void init() throws NamingException {
ClassPathXmlApplicationContext app =new ClassPathXmlApplicationContext("classpath:junit4/InitJndi.xml");
DataSource ds =(DataSource) app.getBean("dataSource");
SimpleNamingContextBuilder builder =new SimpleNamingContextBuilder();
builder.bind("java:/comp/env/jdbc/egrant", ds);
builder.activate();
}
@Test
@Transactional //标明此方法需使用事务
@Rollback(false) //标明使用完此方法后事务不回滚,true时为回滚
public void testAdd() {
System.out.println("测试Spring整合Junit4进行单元测试");
Project prj = new Project();
prj.setZhTitle("Junit4Test");
prjDao.save(prj);
}
}
进行调试后发现是这两个jar有问题,但是不知道为什么,我试着改变pom.xml的jar包顺序
但是还是没有作用。
javaee-web-api-6.0.jar
spring-web-3.2.0.RELEASE.jar
pom文件
spring版本
<spring.version>3.2.0.RELEASE</spring.version>
junit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
j2ee
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>