英文:
Testing with functions passed to a mock object
问题
我有一个简单的函数,最近被一些额外的逻辑包装起来。我在更新测试逻辑方面遇到了困难,因为突然间,方法体被包装在一个模拟对象中。
让我举个例子。
原来的逻辑和测试:
// 逻辑
public void doSomething(Transaction t, int a) {
myService.foo(t, a);
}
// 测试
TestedService service;
@Mock
MyService myService;
@Mock
Transaction t;
@Test
public void testSomething() {
testedService.doSomething(t, 10);
Mockito.verify(myService).foo(t, 10);
}
发生的情况是,我们在一些额外的函数中包装了我们的逻辑:
public void doSomething(Transaction t, int a) {
model.runInEnhancedTransaction(t, t2 -> { myService.foo(t2, a) });
}
我的问题是,当逻辑突然包装在 model
方法中时,我如何测试这个呢(在我的测试中,model
是一个模拟对象)。
基本上,我需要验证当 model
对象是一个模拟对象时,t2 -> { myService.foo(t2, a) }
是否被调用了。
编辑:我已经通过为测试目的实现了我的自定义版本的 model
来使其工作,但仍然想知道是否有一种更加优雅的方法。
英文:
I have a simple function which was recently wrapped by some additional logic. I am struggling with updating the test logic since suddently, the method body is wrapped in a mock.
Let me give you an example.
Former logic & test:
// logic
public void doSomething(Transaction t, int a) {
myService.foo(t, a);
}
And my test:
// test
TestedService service;
@Mock
MyService myService;
@Mock
Transaction t;
@Test
public void testSomething() {
testedService.doSomething(t, 10);
Mockito.verify(myService).foo(t, 10);
}
What happened is that we wrapped our logic in some additional function:
public void doSomething(Transaction t, int a) {
model.runInEnhancedTransaction(t, t2 -> { myService.foo(t2, a) });
}
My question is, how do I test this when the logic is suddently wrapped in in model
method (model is a mock in my test).
I basically need to verify that t2 -> { myService.foo(t2, a) }
was called when the model
object is a mock.
EDIT: I made it work with implementing my custom version of model
just for the test purpose but still wonder if there is some more elegant way.
答案1
得分: 0
以下是您要翻译的内容:
"It's kind of tough to test these kinds of lambda invocations. What I do is perform two tests: one that model.runInEnhancedTransaction()
was called, and one for model.runInEnhancedTransaction()
itself. eg
@Test
void doSomethingCallsModelEnhancedTransaction() {
testedService.doSomething(t, 10);
verify(model).runInEnhancedTransaction(eq(t), any());
}
@Test
void modelRunInEnhancedTransaction() {
Transaction t = mock(Transaction.class);
BiConsumer<Transaction, Integer> consumer = mock(Biconsumer.class);
model.runInEnhancedTransaction(t, consumer);
verify(consumer).accept(...);
}
```"
<details>
<summary>英文:</summary>
It's kind of tough to test these kinds of lambda invocations. What I do is perform two tests: one that `model.runInEnhancedTransaction()` was called, and one for `model.runInEnhancedTransaction()` itself. eg
@Test
void doSomethingCallsModelEnhancedTransaction() {
testedService.doSomething(t, 10);
verify(model).runInEnhancedTransaction(eq(t), any());
}
@Test
void modelRunInEnhancedTransaction() {
Transaction t = mock(Transaction.class);
BiConsumer<Transaction, Integer> consumer = mock(Biconsumer.class);
model.runInEnhancedTransaction(t, consumer);
verify(consumer).accept(...);
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论