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 时模拟被测试方法调用的同一类的方法
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01