Skip to content

Instantly share code, notes, and snippets.

@mechamogera
Created May 22, 2012 12:03
Show Gist options
  • Save mechamogera/2768618 to your computer and use it in GitHub Desktop.
Save mechamogera/2768618 to your computer and use it in GitHub Desktop.
jmokitによる環境変数(System.getenv())の追加
package mymock.test;
import junit.framework.TestCase;
import mockit.Mocked;
import mockit.NonStrictExpectations;
import java.util.Map;
public class SystemTest extends TestCase {
private Map<String, String> envs = null;
public void test() {
envs = System.getenv();
new NonStrictExpectations() {
@Mocked("getenv") System system; // getenv()のみ部分モック
{
System.getenv();
returns(envs);
for (Map.Entry<String, String> env : envs.entrySet()) {
System.getenv(env.getKey());
returns(env.getValue());
}
// 環境変数追加
System.getenv("hoge");
returns("hoge");
}
};
assertEquals("hoge", System.getenv("hoge"); // 追加した環境変数確認
for (Map.Entry<String, String> env : envs.entrySet()) { // 既存の環境変数確認
System.out.println(env.getKey());
System.out.println(System.getenv(env.getKey()));
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment