From 16103c673822689a7c5afb7c85d7f0ef7acde3bb Mon Sep 17 00:00:00 2001 From: Ming Deng Date: Tue, 7 May 2019 10:32:43 +0800 Subject: [PATCH] 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 b95e86215..640b23dd1 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 writableEnv; @Before public void before() { - try { - Map env = System.getenv(); - Class cl = env.getClass(); - Field field = cl.getDeclaredField("m"); - field.setAccessible(true); - writableEnv = (Map) 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 dfe72da43..f9c2967bf 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