나는 powermocking ProcessBuilder
의 생성자에 문제가있어. ProcessBuilder
개체는 조롱되지 않으며 대신 new
은 null
을 반환합니다. 코드 :powermock ProcessBuilder
import java.util.concurrent.Executors;
public class Runner {
private ProcessBuilder processBuilder;
public ProcessBuilder getProcessBuilder() {
return processBuilder;
}
public void run() {
processBuilder = new ProcessBuilder("command", "parameter");
}
public void runInSubclass() {
final SubRunner subRunner = new SubRunner();
subRunner.run();
}
public void runInThread() {
Executors.newSingleThreadExecutor().execute(new SubRunner());
}
private class SubRunner extends Thread {
public void run() {
processBuilder = new ProcessBuilder("command", "parameter");
}
}
}
테스트 (모두 실패) :
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.whenNew;
@RunWith(PowerMockRunner.class)
@PrepareForTest(Runner.class)
public class RunnerTest {
private Runner runner;
@Before
public void setUp() throws Exception {
runner = new Runner();
ProcessBuilder processBuilderMock = mock(ProcessBuilder.class);
whenNew(ProcessBuilder.class).withAnyArguments().thenReturn(processBuilderMock);
}
@Test
public void run() throws Exception {
runner.run();
assertThat(runner.getProcessBuilder(), is(not(nullValue())));
}
@Test
public void runInSubclass() throws Exception {
runner.runInSubclass();
assertThat(runner.getProcessBuilder(), is(not(nullValue())));
}
@Test
public void runInThread() throws Exception {
runner.runInThread();
Thread.sleep(100);
assertThat(runner.getProcessBuilder(), is(not(nullValue())));
}
}
나는 최신 PowerMock (1.6.6)를 사용하고 있습니다.
'문제가 있습니다 ...'제발, 정확히 무슨 일이 일어 났는지, 왜 문제인지 설명해주세요. –
@defaultlocale, updated. new는 null을 반환하고 모든 테스트가 실패합니다. –