[英]TestNG + Spring test: EntityManager return null with testng
我是testng框架的新手。从junit迁移到testng时,entityManager返回null值。它对我来说很奇怪,任何人都可以发现我的错误。 在这里我放置了代码片段。谢谢。
实体
@Entity
@Table(name = "school")
public class School {
private Integer id;
private String schoolName;
private String address;
getters and setters here
}
测试班
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/applicationContext.xml" })
@Transactional
public class SchoolTest {
@PersistenceContext
private EntityManager entityManager;
String address,schoolName;
@BeforeClass
public void init() {
address = "chinna kadai st";
schoolName = "SVM School";
}
@Test
public void save_school() {
School school = new School();
school.setAddress(address);
school.setSchoolName(schoolName);
entityManager.persist(school);
}
}
上面的代码在使用Junit
运行良好
而不是使用@RunWith(SpringJUnit4ClassRunner.class),您需要从AbstractTransactionalTestNGSpringContextTests扩展您的测试类
在Spring Reference docs中阅读更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.