提交 16103c67 编写于 作者: M Ming Deng 提交者: Lemon

Using PowerMockito to mock System.getent; (#2593)

* Using PowerMockito rathen then using reflection to mock System.getent;

* Run Test

* Trigger CI only on the master
上级 4e710857
......@@ -18,37 +18,33 @@
package org.apache.skywalking.apm.commons.datacarrier;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.lang.reflect.Field;
import java.util.Map;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
/**
* @author dengming
* 2019-04-20
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(EnvUtil.class)
public class EnvUtilTest {
private Map<String, String> writableEnv;
@Before
public void before() {
try {
Map<String, String> env = System.getenv();
Class<?> cl = env.getClass();
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
writableEnv = (Map<String, String>) field.get(env);
writableEnv.put("myInt", "123");
writableEnv.put("wrongInt", "wrong123");
writableEnv.put("myLong", "12345678901234567");
writableEnv.put("wrongLong", "wrong123");
} catch (Exception e) {
throw new IllegalStateException("Failed to set environment variable", e);
}
PowerMockito.mockStatic(System.class);
when(System.getenv("myInt")).thenReturn("123");
when(System.getenv("wrongInt")).thenReturn("wrong123");
when(System.getenv("myLong")).thenReturn("12345678901234567");
when(System.getenv("wrongLong")).thenReturn("wrong123");
}
@Test
......@@ -63,14 +59,4 @@ public class EnvUtilTest {
assertEquals(987654321987654321L, EnvUtil.getLong("wrongLong", 987654321987654321L));
}
@After
public void after() {
writableEnv.remove("myInt");
writableEnv.remove("wrongInt");
writableEnv.remove("myLong");
writableEnv.remove("wrongLong");
assertNull(System.getenv("myInt"));
}
}
\ No newline at end of file
......@@ -53,8 +53,8 @@ build_script:
# after_build:
# - mvnw.cmd javadoc:javadoc -Dmaven.test.skip=true
# test_script:
# - mvnw.cmd test
test_script:
- mvnw.cmd test
cache:
- C:\Users\appveyor\.m2
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册