[英]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.