這是我的java類:如何在單元測試期間注入PersistenceContext?
public class Finder {
@PersistenceContext(unitName = "abc")
EntityManager em;
public boolean exists(int i) {
return (this.em.find(Employee.class, i) != null);
}
}
這是單元測試:
public class FinderTest {
@Test public void testSimple() {
Finder f = new Finder();
assert(f.exists(1) == true);
}
}
測試失敗以來Finder.em
NullPointerException
是任何人都無法注入。我應該如何正確處理這種情況?是否存在最佳實踐?
我在每一個單元測試來做到這一點注射「手動」,對不對? – yegor256 2010-09-27 21:00:13
沒有Spring可以解析@PersistenceContext註解並將它注入到Finder類中。您只需引用FinderTest中的ApplicationContext。 – 2010-09-27 21:02:32