Mockito: mocking a method of same class called by method under test when using @InjectMocks(Mockito:在使用@InjectMocks 时模拟被测试方法调用的同一类的方法)
问题描述
我有一个要测试的类,它有几个外部依赖项和几个内部方法.我想为 MethodA
写一个测试,但是 not 有 Method A 对 MethodB
的内部调用来实际执行 MethodB
.我想模拟/存根 MethodB
并返回特定的东西.通常我会使用 when/thenReturn
但它的行为不像我预期的那样 - 它实际上在创建模拟本身时跳转到方法 B.
I have a class I want to test that has several external dependencies, and a couple internal methods. I would like to write a test for MethodA
, but not have Method A's internal call to MethodB
to actually exercise MethodB
. I'd like to mock/stub MethodB
and return something specific instead. Usually I'd use when/thenReturn
but it doesn't behave like I expect - it actually jumps into Method B while creating the mock itself.
MyService.java
@Service
public class MyService {
@Autowired
private ServiceA serviceA;
@Autowired
private ServiceB serviceB;
public SomeObject methodA() {
// some logic using serviceA.method and serviceB.method that creates "output"
SomeObject someObject = methodB(output);
return someObject;
}
public SomeObject methodB(SomeObject someObject) {
// deep mysteries done here to someObject
return someObject
}
}
MyServiceTest.java
public class MyServiceTest {
@Mock
private ServiceA serviceA;
@Mock
private ServiceB serviceB;
@InjectMocks
private MyService myService;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void methodATest() {
when(serviceA.method()).thenReturn(stuff);
when(serviceB.method()).thenReturn(otherStuff);
// here is what I would like to do
when(myService.methodB()).thenReturn(mockedSomeObject); //<- doesn't work
assertThat(myService.methodA().getSomeObjectProperty())
.isEqualTo("property");
}
}
我查看了使用 Mockito.mock(MyService.class)
手动模拟 MyService
类的解决方案,但是(因为上面的示例显然是人为的)我的实际的类有很多外部依赖项,我更喜欢一个解决方案,它仍然允许我使用 @Mock
为 @Autowired
依赖项和 @ 模拟服务InitMocks
用于被测类,除非它根本不可能.
I've looked at solutions that manually mock the MyService
class with Mockito.mock(MyService.class)
, but (as the above example is obviously contrived) my actual class has quite a few external dependencies and I'd prefer a solution that still allows me to mock the service using @Mock
for the @Autowired
dependencies and @InitMocks
for the class under test, unless it's simply not possible.
我试过了:
Mockito.doReturn(mockedSomeObject).when(myService.methodB(any(SomeObject.class));
但在为该方法创建模拟时也会进入 MethodB,这不应该发生.
but that also steps into MethodB when creating the mock for that method, which shouldn't be happening.
提前感谢您的帮助!
推荐答案
尝试将@Spy 添加到您的 InjectMocks 并使用该对象以稍微不同的语法预期"它们.
Try Adding @Spy to your InjectMocks and use the object to "expect" them in a slightly different syntax.
导入 org.mockito.Spy;
@InjectMocks
@Spy
private MyService myService;
现在模拟服务调用
Mockito.doReturn(mockedSomeObject).when(myService).methodB();
还将其他模拟调用更改为此
Also change the other mock call to this
Mockito.doReturn(stuff).when(serviceA).method();
Mockito.doReturn(otherStuff).when(serviceB).method();
这篇关于Mockito:在使用@InjectMocks 时模拟被测试方法调用的同一类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mockito:在使用@InjectMocks 时模拟被测试方法调用的同一类的方法


基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13