单元测试时静态元模型属性为空

2022-09-04 03:57:14

我在一个外部包中@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。

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


答案 1

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

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

答案 2

与其创建自己的类,我建议让Mockito为你做这项工作。

 @Mock // declare a mock along the others you might have
 private SingularAttribute<BookingSystem, Integer> code;

 @Before
 public void setUp() throws Exception { 
     // fill metamodel with it
     BookingSystem_.code = code;
 }

值得一提的是,将元模型引入服务层并不是很好的做法,你宁愿将它们推到DAO方法。


推荐