简体   繁体   中英

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 . 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

Instead of using @RunWith(SpringJUnit4ClassRunner.class), you need to extend your test class from AbstractTransactionalTestNGSpringContextTests

Read here for more info in Spring Reference docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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