PowerMock 使用方法
環境情報
- Junit-4.12
- PowerMock1.6.2
domain/pom.xml(抜粋)
<!-- https://mvnrepository.com/artifact/com.jcraft/jsch -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.powermock/powermock -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-legacy</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
テストクラス
package training.domain.service;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(CallJsch.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:test-context.xml"})
public class CallJschTest {
@Mock
private JschTraining mock;
@InjectMocks
public CallJsch target = spy(new CallJsch());
@Test
public void test1() throws Exception {
{
MockitoAnnotations.initMocks(this);
PowerMockito.whenNew(JschTraining.class).withAnyArguments().thenReturn(mock);
PowerMockito.when(mock.exec(any())).thenReturn("SUCCESS");
}
target = new CallJsch();
String str = target.call("root", "127.0.0.0", 22, "Administrator", 10);
assertThat(str ,is("SUCCESS"));
}
}
テスト対象
package training.domain.service;
import java.io.IOException;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
public class CallJsch {
public String call(String user, String host, int port, String password, int timeout)
throws JSchException, IOException, SftpException, Exception {
JschTraining target = new JschTraining(user, host, port, password, timeout);
return target.exec("ECHO TEST");
}
}
テスト対象が呼び出すクラス
package training.domain.service;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class JschTraining {
JSch jsch;
Session session;
ChannelExec channel;
public JschTraining(String user, String host, int port, String password, int timeout) throws JSchException {
jsch = new JSch();
session = jsch.getSession(user, host, port);
// known_hostsのチェックをスキップ
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.setTimeout(timeout);
session.connect();
channel = (ChannelExec) session.openChannel("exec");
}
public String exec(String command) throws JSchException, SftpException, FileNotFoundException, IOException {
BufferedInputStream bin = null;
ByteArrayOutputStream bout = null;
try {
channel.setCommand(command);
channel.connect();
// コマンド実行
bin = new BufferedInputStream(channel.getInputStream());
bout = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int length;
while (true) {
length = bin.read(buf);
if (length == -1) {
break;
}
bout.write(buf, 0, length);
}
// 標準出力
System.out.format("実行結果=%1$s", new String(bout.toByteArray(), StandardCharsets.UTF_8));
} finally {
if (bin != null) {
try {
bin.close();
} catch (IOException e) {
}
}
if (channel != null) {
try {
channel.disconnect();
} catch (Exception e) {
}
}
if (session != null) {
try {
session.disconnect();
} catch (Exception e) {
}
}
}
return new String(bout.toByteArray(), StandardCharsets.UTF_8);
}
}
ポイント
@PrepareForTest(CallJsch.class) ←ここは試験対象のクラス
@Mock
private JschTraining mock; ←ここは試験対象が呼び出す他のクラス