如何模拟一个字段设置为 System.getEnv("...") 的类?

问题描述 投票:0回答:1

我正在尝试为一个变量值设置为 System.getEnv("...") 的类编写单元测试。现在,我在运行测试类时遇到初始化错误。我尝试过模拟 System 类,但没有成功。这里可以做什么来运行测试类? enter image description hereenter image description here

 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 at org.mockito.internal.util.reflection.ReflectionMemberAccessor.lambda$newInstance$0(ReflectionMemberAccessor.java:29)
 at org.mockito.internal.util.reflection.ReflectionMemberAccessor.newInstance(ReflectionMemberAccessor.java:29)
 at org.mockito.internal.util.reflection.ReflectionMemberAccessor.newInstance(ReflectionMemberAccessor.java:20)
 at org.mockito.internal.util.reflection.ModuleMemberAccessor.newInstance(ModuleMemberAccessor.java:42)
 at org.mockito.internal.util.reflection.FieldInitializer$ParameterizedConstructorInstantiator.instantiate(FieldInitializer.java:287)
 at org.mockito.internal.util.reflection.FieldInitializer.acquireFieldInstance(FieldInitializer.java:146)
 at org.mockito.internal.util.reflection.FieldInitializer.initialize(FieldInitializer.java:91)
 at org.mockito.internal.configuration.injection.ConstructorInjection.processInjection(ConstructorInjection.java:48)
 at org.mockito.internal.configuration.injection.MockInjectionStrategy.process(MockInjectionStrategy.java:68)
 at org.mockito.internal.configuration.injection.MockInjectionStrategy.relayProcessToNextStrategy(MockInjectionStrategy.java:91)
 at org.mockito.internal.configuration.injection.MockInjectionStrategy.process(MockInjectionStrategy.java:71)
 at org.mockito.internal.configuration.injection.MockInjection$OngoingMockInjection.apply(MockInjection.java:88)
 at org.mockito.internal.configuration.DefaultInjectionEngine.injectMocksOnFields(DefaultInjectionEngine.java:26)
 at org.mockito.internal.configuration.InjectingAnnotationEngine.injectCloseableMocks(InjectingAnnotationEngine.java:118)
 at org.mockito.internal.configuration.InjectingAnnotationEngine.processInjectMocks(InjectingAnnotationEngine.java:61)
 at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:48)
 at org.mockito.MockitoAnnotations.openMocks(MockitoAnnotations.java:81)
 at org.mockito.internal.framework.DefaultMockitoSession.(DefaultMockitoSession.java:43)
 at org.mockito.internal.session.DefaultMockitoSessionBuilder.startMocking(DefaultMockitoSessionBuilder.java:83)
 at org.mockito.junit.jupiter.MockitoExtension.beforeEach(MockitoExtension.java:153)
 at java.util.ArrayList.forEach(ArrayList.java:1259)
 at java.util.ArrayList.forEach(ArrayList.java:1259)

 Suppressed: java.lang.NullPointerException
 at org.mockito.junit.jupiter.MockitoExtension.afterEach(MockitoExtension.java:184)
 at java.util.ArrayList.forEach(ArrayList.java:1259)
  ... 47 more
Caused by: java.lang.NullPointerException
 at com.sixsense.orchestration.minionlib.service.PreviewCommonService.(PreviewCommonService.java:29)
 ... 77 more

enter image description here

java spring unit-testing mockito
1个回答
0
投票

静态代码块在java类加载期间执行。静态代码块在 Java 类加载期间执行。在您的代码中,

PreviewCommonService.BUCKET = System.getenv(SIXSENSE_ENV_STR)...
甚至会在测试开始之前执行,即在模拟生效之前。据我所知,没有可行的编程方法来防止这种情况发生。

无论如何,可以通过不将

BUCKET
a 设置为静态字段来改进实现,以便稍后可以对其进行设置。或者,您可以使用
System.getProperties()
代替,这样更容易使用
-D
设置。

要运行测试,您需要将环境变量 SIXSENSE_ENV_STR 添加到运行命令中。

  1. 要使用 IntelliJ 运行,只需将环境变量添加到您的运行/调试配置中: Run with IntelliJ IDEA IDE
  1. 要从命令行使用 mvn 或 gradle 运行,只需在执行它们之前添加环境变量:
  • Windows
    设置 SIXSENSE_ENV=测试
  • Linux/Unix
    SIXSENSE_ENV=测试
  1. 您可以在
    surefire-plugin
    配置中添加环境变量。以Maven为例:
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <forkNode>always</forkNode> <!-- THIS IS IMPORTANT. surefire will fork new processes so that the environment variable could take effect -->
        <environmentVariables>
            <SIXSENSE_ENV>test</SIXSENSE_ENV>
        </environmentVariables>
     </configuration>
</plugin>
...
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.