我在也具有静态元模型的外部包中有@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 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”中包。

我在代码中使用了所有bean和注释驱动的配置(没有持久性或上下文xml文件)。就测试而言,我正在IntelliJ中使用TestNG和PowerMock。

看起来好像在单元测试期间没有选择元模型。有任何想法吗。

最佳答案

加载 hibernate 时,将填充静态元模型类。因此,您可以在测试中配置 hibernate 上下文,或者在执行方法之前手动填充属性。在您的代码中,您可以执行以下操作:

@Test
public void doIt() {
    BookingSystem_.code = new SingularAttributeMock<BookingSystem, Integer>();
    bookingService.saveBooking(booking);
  }
}

可以定制类 SingularAttributeMock 以便在测试中使用它。您还可以使用 SingularAttribute 类的任何其他实现。
public class SingularAttributeMock<X, Y> implements SingularAttribute<X, Y> {
   //Overriding methods of SingularAttribute...
}

09-26 17:06