PowerMockでEasyMockを使用したソリューションは次のとおりです。
TestClass.java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ A.class, B.class })
public class TestClass {
@Test
public void testDoSomeThing() throws Exception {
/* Setup */
B bMock = PowerMock.createMock(B.class);
/* Mocks */
PowerMock.expectNew(B.class).andReturn(bMock).atLeastOnce();
bMock.callMe();
/* Activate */
PowerMock.replayAll();
/* Test */
A cut = new A();
cut.doSomething();
/* Asserts */
PowerMock.verifyAll();
}
}
A.java
public class A {
B b = new B();
public void doSomething() {
b.callMe();
}
}
B.java
public class B {
public void callMe() {
}
}