gpt4 book ai didi

java - TestNG + Spring 测试 : EntityManager return null with testng

转载 作者:行者123 更新时间:2023-11-30 03:32:30 25 4
gpt4 key购买 nike

我是 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时运行良好

最佳答案

您需要从 AbstractTransactionalTestNGSpringContextTests 扩展测试类,而不是使用 @RunWith(SpringJUnit4ClassRunner.class)

阅读此处了解更多信息 Spring Reference docs .

关于java - TestNG + Spring 测试 : EntityManager return null with testng,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28690416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com