admin管理员组文章数量:1124387
I am struggling to mock a void method which is written inside the inject mocked class which i need to write junit for.
Maths.java
@Component
public class Maths {
@Autowired
private Data data;
public String getData(){
////Some code block
String value = data.someMethod();
addTest("test");
return value;
}
public void addTest(String text){
////Some code block
}
}
MathsTest.java
@RunWith(MockitoJUnitRunner.class)
public class MathsTest{
@InjectMocks
private Maths maths;
@Mock
private Data data;
@Test
public void testGetData(){
Mockito.when(data.someMethod).thenReturn("someValue");
assertNotNull(maths.getData());
}
}
Now in the above test case when maths.getData()
is called I want to mock or ignore the code block addTest()
. I have tried using @Spy
on Maths class but that completely ignore the getData()
method.
If i use doNothing() then since the Maths class is not mocked, i get stubbing error. Requesting help as of how to achieve this?
本文标签: unit testingMocking a void method inside inject mock classStack Overflow
版权声明:本文标题:unit testing - Mocking a void method inside inject mock class - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736629231a1945740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论