我想验证注入(inject)的依赖方法是否使用不同的参数类型调用了两次。所以假设我的类(class)是:
public class MyClass {
@PersistenceContext(name = "PU")
EntityManager entityManager;
public void doSomething() {
Customer customer = new Customer();
Address customerAddress = new Address;
entityManager.persist(customer)
entityManager.persist(customerAddress);
}
}
@PersistenceContext 是 Java EE 特定注释,用于告诉应用程序服务器为特定持久性单元注入(inject) EntityManager。
所以我想测试 persist 被调用两次,一次传递一个 Customer 对象,另一次传递一个 Address 对象。
创建以下测试类通过:
public class MyClassTests {
@Tested
MyClass myClass;
@Injectable
EntityManager entityManager;
@Test
public void TestPersistCustomerAndAddress() {
new Expectations() {{
entityManager.persist(withAny(Customer.class));
entityManager.persist(withAny(Address.class));
}};
myClass.doSomething();
}
}
然而,JMockit 似乎忽略了传递给 withAny 的类类型。我基本上可以将 withAny 更改为 withAny(Date.class) 并且测试仍然会通过。
有没有办法验证传递给持久化的特定对象类型?
最佳答案
您是否尝试过使用withInstanceOf(Customer.class)?我认为这应该可以满足您的要求。
关于java - JMockit 验证使用不同参数调用两次的可注入(inject)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39040532/