You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by li...@apache.org on 2019/05/07 02:32:48 UTC
[skywalking] branch master updated: Using PowerMockito to mock
System.getent; (#2593)
This is an automated email from the ASF dual-hosted git repository.
liuhaoyangzz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git
The following commit(s) were added to refs/heads/master by this push:
new 16103c6 Using PowerMockito to mock System.getent; (#2593)
16103c6 is described below
commit 16103c673822689a7c5afb7c85d7f0ef7acde3bb
Author: Ming Deng <mi...@qq.com>
AuthorDate: Tue May 7 10:32:43 2019 +0800
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
---
.../apm/commons/datacarrier/EnvUtilTest.java | 44 ++++++++--------------
appveyor.yml | 4 +-
2 files changed, 17 insertions(+), 31 deletions(-)
diff --git a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java
index b95e862..640b23d 100644
--- a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java
+++ b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java
@@ -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
diff --git a/appveyor.yml b/appveyor.yml
index dfe72da..f9c2967 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -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