मेरे पास बाहरी पैकेज में @Entity कक्षाएं हैं जिनमें स्थिर मेटामोडल्स भी हैं। मेरे आवेदन की सेवा कक्षा में, मैं अपने डेटा को पुनर्प्राप्त करने के लिए उन मेटामोडल्स और EntityManager/CriteriaBuilder/CriteriaQuery का उपयोग कर रहा हूं। एप्लिकेशन चलाने पर यह ठीक काम करता है। हालांकि, जब यूनिट परीक्षण चलाते हैं, तो मेरे मेटामोडल्स और उनके गुण हमेशा शून्य होते हैं।स्टेटिक मेटामोडेल शून्य की विशेषता देता है जब यूनिट परीक्षण
कोड ... मेरे एप्लिकेशन की सेवा वर्ग में
package com.example.core.entities;
@Entity
@Table(schema = "lookup", name="BookingSystem")
public class BookingSystem implements ILookupEntity, IAuditEntity, Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id")
public Integer id;
@Column(name = "name")
public String name;
@Column(name = "code")
public Integer code;
}
package com.example.core.entities;
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(BookingSystem.class)
public abstract class BookingSystem_ {
public static volatile SingularAttribute<BookingSystem, Integer> id;
public static volatile SingularAttribute<BookingSystem, Integer> code;
public static volatile SingularAttribute<BookingSystem, String> name;
}
प्रयोग ...
package com.example.bookingsystem;
@Service
public class BookingService {
@PersistenceContext
private EntityManager entityManager;
public void saveBooking(Booking booking) {
//...
RepositoryQueryBuilder<BookingSystem> bookingSystemSelector = new RepositoryQueryBuilder<>(entityManager, BookingSystem.class);
List<BookingSystem> bookingSystems = bookingSystemSelector
.and(BookingSystem_.code, booking.bookingSystem.code) //<-- Here "BookingSystem_.code" is null.
.getResultList();
//...
}
}
"RepositoryQueryBuilder" वर्ग है सिर्फ एक उपयोगिता बिल्डर वर्ग कि लपेटता एक EntityManager, CriteriaBuilder, आदि । मूल रूप से इस उदाहरण के बाद मॉडलिंग की ... JPA Criteria Predicate Conditions
यूनिट परीक्षण कोड ...
package com.example.bookingsystem;
public abstract class BaseTestSetup {
@InjectMocks
protected BookingService bookingService;
protected EntityManager entityManager = PowerMockito.mock(EntityManager.class);
protected CriteriaBuilder criteriaBuilder = PowerMockito.mock(CriteriaBuilder.class);
protected CriteriaQuery<BookingSystem> criteriaQuery = PowerMockito.mock(CriteriaQuery.class);
protected Root<BookingSystem> root = PowerMockito.mock(Root.class);
protected void arrange() {
when(entityManager.getCriteriaBuilder()).thenReturn(criteriaBuilder);
when(criteriaBuilder.createQuery(BookingSystem.class)).thenReturn(criteriaQuery);
when(criteriaQuery.from(Matchers.<Class<BookingSystem>>any())).thenReturn(root);
when(criteriaQuery.from(Matchers.<EntityType<BookingSystem>>any())).thenReturn(root);
}
}
@RunWith(PowerMockRunner.class)
public class BookingServiceTest extends BaseTestSetup {
@BeforeClass
@Override
public void arrange() {
super.arrange();
//...
}
@Test
public void doIt() {
Booking booking = new Booking();
booking.id = 12345;
booking.bookingSystem = new BookingSystem();
booking.bookingSystem.id = 1;
booking.bookingSystem.code = 106000;
bookingService.saveBooking(booking);
}
}
मैं इस JPA/Hibernate Static Metamodel Attributes not Populated -- NullPointerException को देखा है, लेकिन समाधान "लगता है कि इकाई और उसके मेटामॉडल एक ही पैकेज में कर रहे हैं" प्रतीत हो रहा है, लेकिन जैसा कि आप देख सकते हैं, दोनों पहले से ही मेरे "com.example.core.entities" पैकेज में हैं।
मैं अपने कोड (सभी दृढ़ता या संदर्भ xml फ़ाइलों) में सभी बीन और एनोटेशन संचालित कॉन्फ़िगरेशन का उपयोग कर रहा हूं। जहां तक परीक्षण जाता है, मैं इंटेलिजे के भीतर से टेस्टएनजी और पावरमैक का उपयोग कर रहा हूं।
ऐसा लगता है जैसे यूनिट परीक्षणों के दौरान मेटामोडल्स नहीं उठाए जा रहे हैं। कोई विचार।
ऐसा लगता है कि जेपीए प्रोसेसर को सिंगुलरएट्रिब्यूट गुणों के उदाहरण प्रदान करना चाहिए। क्या आपको इकाई परीक्षण के लिए कोई समाधान मिला? – srnjak
मुझे एक समान समस्या है। मैं स्टेटिक्स का उपयोग कर रहा हूं लेकिन मानदंडों के बिना, जैसे: 'ओ ओआईडी, ओ। "+ Entity_.attribute.getName() +" एंटीटी ओ से ... ' मेरे पास 2 टेस्ट हैं, लेकिन पहला एक अच्छा काम करता है, लेकिन दूसरा स्थिर मेटा मॉडल को पहचानता नहीं है और एनपीई फेंकता है – FiruzzZ