单元测试时静态元模型属性为空
我在一个外部包中@Entity类,这些类也具有静态元模型。在应用程序的服务类中,我使用这些元模型和 EntityManager/CriteriaBuilder/CriteriaQuery 来检索我的数据。这在运行应用程序时工作正常。但是,在运行单元测试时,我的元模型及其属性始终为 null。
法典。。。
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 标准谓词条件
单元测试代码...
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属性未填充 - NullPointerException,但解决方案似乎是“确保实体及其元模型在同一包中”,但正如你所看到的,两者都已经在我的“com.example.core.entities”包中。
我在代码中使用了所有bean和注释驱动的配置(没有持久性或上下文xml文件)。就测试而言,我正在IntelliJ中使用TestNG和PowerMock。
看起来好像在单元测试期间没有拾取元模型。任何想法。