[英]Mock a nested class with constructor parameters and test the methods
I am trying to Mock a class which has a nested class. 我正在尝试模拟一个具有嵌套类的类。 That nested class was with a constructor argument. 该嵌套类带有构造函数参数。 When I am trying to test using mockito instead of mocking the actual method is getting executed. 当我尝试使用模拟而不是模拟进行测试时,实际方法正在执行。
I have done @InjectMocks on outer class and @Mock of the inner class. 我在外部类和内部类的@Mock上完成了@InjectMocks。
//Actual Class to test using Mockito.
public class ClassA {
public void initMethod(String s1, String s2, String s3, String s4) throws Exception {
ClassB objB = null;
if (objB == null && s3 != null && s4 != null && s2 != null) {
SampleUtil.KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(s3, s4);
objB = new ClassB(s1, s2, pair.keyStore, pair.keyPassword);
try {
objB.meth1(); //Note: meth1 and meth2 are void methods.
objB.meth2(); // These two methods only to be accessed. something like doNothing
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Which I have tried as usual to call the class using @Mock but the actual method meth1() is getting accessed. 我像往常一样尝试使用@Mock调用类,但是实际的方法meth1()正在访问。
//Somthing which I tried
@RunWith(MockitoJUnitRunner.class)
public class MockitoTest {
@InjectMocks
ClassA classA;
@Mock
ClassB classB;
@Before
public void initMocks() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testInitClient() throws Exception {
// Setup
// Run the test
classA.initMethod("Lorem", "Ipsum", "TestStr1", "TestStr2");
doNothing().when(classB).meth1(); // This is the line need to be mocked. But instead calling the actual method and executing
// Verify the results
}
The inner ClassB method needs to be mocked instead of accessing the real method. 内部的ClassB方法需要被模拟,而不是访问实际的方法。
As a beginner to mockito I am trying to clear this. 作为mockito的初学者,我正在尝试清除此问题。 But got confused on few points like accessing void method, SO can't use when then. 但是在访问void方法等几点上感到困惑,因此那时无法使用。 Accessing constructor with parameter etc., 使用参数等访问构造函数,
I don't think this is possible without some kind of dependency injection. 如果没有某种依赖注入,我认为这是不可能的。
Here, I modified your code so that it behaves as you wanted. 在这里,我修改了您的代码,使其行为如您所愿。
public class ClassA {
// Needed so that it can be replaced with setter
private ClassB objB;
// Extract the generation of ClassB so that it can be prevented
public void generateClassB(String s1, String s2) {
this.objB = new ClassB(s1, s2);
}
public void initMethod(String s1, String s2, String s3, String s4) {
objB = null;
if (s3 != null && s4 != null && s2 != null) {
SampleUtil.KeyStorePasswordPair pair = SampleUtil.getKeyStorePasswordPair(s3, s4);
generateClassB(s1, s2);
try {
objB.meth1(); //Note: meth1 and meth2 are void methods.
objB.meth2(); // These two methods only to be accessed. something like doNothing
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void setClassB(ClassB classB) {
this.objB = classB;
}
}
This is the implementation of ClassB that I used in order to test the results. 这是我用来测试结果的ClassB的实现。
public class ClassB {
private String s1;
private String s2;
public ClassB(String s1, String s2) {
this.s1 = s1;
this.s2 = s2;
}
public void meth1() {
System.out.println(s1);
}
public void meth2() {
System.out.println(s2);
}
}
And the test file 和测试文件
@RunWith(MockitoJUnitRunner.class)
public class DemoApplicationTests {
private ClassA classA;
private ClassB classB;
@Test
public void testInitClient() {
classA = Mockito.spy(ClassA.class);
classB = Mockito.spy(new ClassB("a", "b"));
Mockito.doNothing()
.when(classB)
.meth1();
// This will replace the ClassA#generateClassB method call with the setter
Mockito.doAnswer(args -> {
classA.setClassB(classB);
return null;
}).when(classA).generateClassB(Mockito.any(), Mockito.any());
classA.initMethod("a", "b", "c", "d");
}
}
An alternative, and much cleaner, solution would be to pass ClassB instance to ClassA#initMethod. 另一种更清洁的解决方案是将ClassB实例传递给ClassA#initMethod。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.