Quantcast
Channel: プログラム の個人的なメモ
Viewing all articles
Browse latest Browse all 860

【JUnit】 JUnit で環境変数を変更するテストを行うには...

$
0
0

■ JUnit で環境変数を変更するテストを行うには...

 * 以下の「System Rules」を使うと実現可能。
http://stefanbirkner.github.io/system-rules/

■ 設定

http://stefanbirkner.github.io/system-rules/download.html
に記載。今回は、Gradleを使う。

build.gradle

dependencies {
    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
    
    // System Rules
    testCompile 'com.github.stefanbirkner:system-rules:1.17.0'
}

■ サンプル

import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

public class SampleTest {
  @Rule
  public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

  @Test
  public void test() {
    environmentVariables.set("ENV_KEY", "dummy01");

    assertEquals("dummy01", System.getenv("ENV_KEY"));
  }
}

■ 使用上の注意

 * 環境変数を設定したら、別のテストメソッドにも反映される(以下の「サンプル」参照)

サンプル

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

public class SampleTest {

  @Test
  public void test1() {
    EnvironmentVariables environmentVariables = new EnvironmentVariables();
    environmentVariables.set("ENV_KEY", "dummy01");

    assertEquals("dummy01", System.getenv("ENV_KEY"));
  }

  @Test
  public void test2() {
    assertNull(System.getenv("ENV_KEY")); // ★エラー★(test1()で設定した値「dummy01」が返ってくる)
  }
}

■ 補足 : PowerMockito の使用について

 * PowerMockito で行うことができることもネットでは書いてあるが、
   エラー「MissingMethodInvocationException」になった

行ったこと

 * 以下の通り。(足りない点があるかもしれないが...)
build.gradle
dependencies {
    // PowerMockito
    testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.7.3'
    testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '1.7.3'
}
JUnitコード
import static org.junit.Assert.*;

import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;

public class SampleTest {

  @Test
  public void test() {
    PowerMockito.mockStatic(System.class);
    PowerMockito.when(System.getenv("ENV_KEY")).thenReturn("dummy01");

    assertEquals("dummy01", System.getenv("ENV_KEY"));
  }
}
エラー内容
org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

	at com.sample.db.SampleTest.test(SampleTest.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
https://stackoverflow.com/questions/8168884/how-to-test-code-dependent-on-environment-variables-using-junit?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
より抜粋
~~~~~
when(System.getenv("FOO_VAR_1")).thenReturn("test-foo-var-1") causes org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. error – Andremoniy Oct 24 '17 at 13:14
~~~~~

同じ現象...


関連記事

【JUnit】 DBにまつわる単体試験 [3] ~ AssertJ / AssertJ-DB ~

https://blogs.yahoo.co.jp/dk521123/36157721.html

Viewing all articles
Browse latest Browse all 860

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>