【问题标题】:TestNG + Spring test: EntityManager return null with testngTestNG + Spring 测试:EntityManager 使用 testng 返回 null
【发布时间】:2015-02-24 07:37:47
【问题描述】:

我是 testng 框架的新手。从 junit 迁移到 testng 时,entityManager 返回空值。它对我来说看起来很奇怪,谁能发现我的错误。我在这里放了我的代码 sn-ps。谢谢。

实体

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

【问题讨论】:

    标签: java spring testng spring-test


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多