英文:
Mockito: When any instance invokes method, thenReturn something
问题
以下是您要翻译的内容:
我对Mockito非常陌生,有一个类似这样的类:
public class A {
private B b;
public B getB() { return b; }
// ...
}
和
public class B {
private C c;
public C getC() { return c; }
// ...
}
我正在使用Mockito为类A编写单元测试,并尝试实现以下行为,其中B是一个模拟对象:
when(a.getB().getC()).thenReturn(mock(C.class))
when(any(mock(C.class)).doSomething()).thenReturn("somethingElse") // 这不起作用。
如何在任何模拟的C类实例调用doSomething()时返回"somethingElse"?我不想将此行为绑定到任何一个特定的mock(C.class)实例。
请注意,我已经将HTML编码(例如"
)还原为正常的引号。如果您需要进一步的帮助,请告诉我。
英文:
I'm very new to Mockito and have a class like
public class A {
private B b;
public B getB() { return b; }
// ...
}
and
public class B {
private C c;
public C getC() { return c; }
// ...
}
I am writing unit tests using Mockito for class A, and trying to achieve the following behavior, where B is a mock:
when(a.getB().getC()).thenReturn(mock(C.class))
when(any(mock(C.class)).doSomething()).thenReturn("somethingElse") // This doesn't work.
How can I return "somethingElse" when any mocked instance of C calls doSomething()? I don't want to bind this behavior to any one particular mock(C.class) instance.
答案1
得分: 1
以下是翻译好的部分:
首先,您必须将测试对象视为一个单元,并模拟其他一切。所以,因为您正在测试类A
,您不能模拟A
的方法。
这是如何使用Mockito使用注解来模拟B
实例的方法:
@MockitoSettings
public class ATest {
@Mock
private B b;
@InjectMocks
private A a;
@Test
void testExample() {
C c = mock(C.class);
when(b.getC()).thenReturn(c);
when(c.doSomething()).thenReturn("somethingElse");
// a.getB() == b
}
}
那么这里发生了什么呢?
注解@Mock
正在初始化一个类型为B
的模拟对象,并将字段变量b
设置为它。
@InjectMocks
正在收集所有@Mock
字段,并设置对象中的字段。这是您的测试对象。
现在,您的A
实例有一个模拟的B
对象。而在B
内部,还有一个C
。由于B
是一个模拟对象,方法getC()
实际上什么都不做。您必须为它设置一些功能。在测试中,我初始化了一个本地的模拟C
实例,以便从getC()
返回。
因此,现在当A
对象访问模拟的B
并获取C
时,它将获得这个本地变量c
以供使用。因此,您可以为这个对象设置模拟函数。
英文:
First of all, you have to treat your test subject as a unit, and mock everything else. So since you're testing class A
you cannot mock A
s methods.
Here's how you would use Mockito to mock the instance of B
using annotations:
@MockitoSettings
public class ATest {
@Mock
private B b;
@InjectMocks
private A a;
@Test
void testExample() {
C c = mock(C.class):
when(b.getC()).thenReturn(c);
when(c.doSomething()).thenReturn("somethingElse");
// a.getB() == b
}
}
So what is happening here?
The annotation @Mock
is intializing a mocked object of type B
and setting the field variable b
to it.
@InjectMocks
is collecting all @Mock
fields and setting the fields in the object. This is your test subject.
Now your A
instance has a mocked B
object. And within B you have C
. Since B
is a mocked object the method getC()
doesn't do anthing at all. You have to stub it with some functionality. In the test, I've initialized a local mocked C
instance to be returned from getC()
.
So now when A
object accesses the mocked B
and get C
it will get this local variable c
to use. So you can stub this object with mocked functions.
答案2
得分: 0
不要添加mock()
,它用于创建新的模拟对象。
像这样:
when(any(C.class).doSomething()).thenReturn("somethingElse");
英文:
Do not add the ```mock()`` its used for creating new mocks.
Like this
when(any(C.class).doSomething()).thenReturn("somethingElse");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论