简体   繁体   English

TestNG +春季测试:EntityManager使用testng返回null

[英]TestNG + Spring test: EntityManager return null with testng

I am new to testng framework .while migrating to testng from junit ,entityManager return null value .its looks strange for me can any one spot my mistake . 我是testng框架的新手。从junit迁移到testng时,entityManager返回null值。它对我来说很奇怪,任何人都可以发现我的错误。 here i placed my code snippets.thank you . 在这里我放置了代码片段。谢谢。

Entity 实体

@Entity
@Table(name = "school")
public class School {
    private Integer id;
    private String schoolName;
    private String address;

    getters and setters here
}

Test class 测试班

@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);
}
}

the above code is working well while using Junit 上面的代码在使用Junit运行良好

Instead of using @RunWith(SpringJUnit4ClassRunner.class), you need to extend your test class from AbstractTransactionalTestNGSpringContextTests 而不是使用@RunWith(SpringJUnit4ClassRunner.class),您需要从AbstractTransactionalTestNGSpringContextTests扩展您的测试类

Read here for more info in Spring Reference docs . Spring Reference docs中阅读更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM