Mockito PowerMockito.
, ClassUnderTest
public class ClassUnderTest {
String name;
boolean condition;
public ClassUnderTest(String name, boolean condition) {
this.name = name;
this.condition = condition;
init();
}
...
}
,
public class MyClass {
public MyClass() { }
public void createCUTInstance() {
ClassUnderTest cut = new ClassUnderTest("abc", true);
}
...
}
Test ...
(1) PowerMockRunner PrepareForTest:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ ClassUnderTest.class, MyClass.class })
public class TestClass {
(2) , :
@Before
public void setup() {
ClassUnderTest cutMock = Mockito.mock(ClassUnderTest.class);
PowerMockito.whenNew(ClassUnderTest.class)
.withArguments(Matchers.anyString(), Matchers.anyBoolean())
.thenReturn(cutMock);
}
(3) :
@Test
public void testMethod() {
MyClasss myClass = new MyClass();
myClass.createCUTInstance();
String name = "abc";
String condition = true;
PowerMockito.verifyNew(ClassUnderTest.class).withArguments(name, condition);
}