JMockit 验证使用不同参数调用两次的可注入方法
JMockit verify injectable method called twice with different arguments
我想验证注入的依赖方法是否使用不同的参数类型被调用了两次。所以假设我的 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 特定注释,用于告知应用程序服务器为特定持久性单元注入 EntityManager。
所以我想测试 persist 被调用了两次,一次被传递给 Customer 对象,另一次被传递给 Address 对象。
创建以下测试 class 通过:
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 的 class 类型。我基本上可以将 withAny 更改为 withAny(Date.class) 并且测试仍然会通过。
有没有办法验证传递给持久化的特定对象类型?
您尝试过使用 withInstanceOf(Customer.class) 吗?我认为这应该可以满足您的要求。
我想验证注入的依赖方法是否使用不同的参数类型被调用了两次。所以假设我的 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 特定注释,用于告知应用程序服务器为特定持久性单元注入 EntityManager。
所以我想测试 persist 被调用了两次,一次被传递给 Customer 对象,另一次被传递给 Address 对象。
创建以下测试 class 通过:
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 的 class 类型。我基本上可以将 withAny 更改为 withAny(Date.class) 并且测试仍然会通过。
有没有办法验证传递给持久化的特定对象类型?
您尝试过使用 withInstanceOf(Customer.class) 吗?我认为这应该可以满足您的要求。