地球環境変数を設定する方法際にユニットテスト

iamjoshua:

私はそれを実行するたびに、私が遭遇した、しかし、ユニットテストを作成しようとしていますnull私の変数に値を。私のユニットテストでグローバルに使用されていない私の環境変数に問題点をデバッグします。class私が呼び出し、次に環境変数から値を参照する別のクラスをテストしています。どのように私はIntelliJの中に地球環境変数の使用を作るのですか?。

私はすでに、このようなテスト構成で環境変数を追加しました

しかし、これは、プロジェクト全体で使用されていないようです。したがって、この環境変数を呼び出すクラスがnullを返しています。どのように私は私のユニットテストし、それがグローバルに宣言環境変数を使用するために呼び出すクラスを作るのですか?。TIA。以下は、私の環境変数は、クラスで使用されているかの私のコードのスニペットです

@Value("${fintech.clientid}")
private String clientId;
@Value("${fintech.secret}")
private String clientSecret;
@Value("${digifi-host}")
private String fintechHost;
@Value("${fintech-digifi-login-endpoint}")
private String loginUrl;
@Value("${fintech-single-session-endpoint}")
private String singleSessionUrl;

これが何をするか、それは私のapplication.propertiesファイルに格納された値を呼び出して、クラスからです。そして、application.propertiesファイルから、それは実行時に宣言された環境変数で適切な値を探すためにしようとします。

So from Java Class > application.properties file/config > Runtime Environment Variables 

テストをデバッグする場合は、以下のnull値を持つ変数のスクリーンショットです。あなたが見ることができるように、すべての値は、それが私はユニットテストに入れている環境変数をロードしませんでした意味はnullです。(私はここでは答えに入れて)私は一時的な修正を持っていた他のテストケースでは、彼らは人口ので、適切に環境変数をロードしますが、このような私の他の多くのテストケースでは、それはしていませんされています。

ここでは、画像の説明を入力します。

PS: I've already found related articles in stackoverflow, but they are all test-class specific and that uses surefire plugins or setting the environment variables or via pom, but I don't need it to be there as it is a requirement for us to use environment variables on runtime as the values of the variables should be hidden and not visible on the code. I just simply need the entire project to use a global environment variable when it is doing its Unit Test. Much like how my project would use the environment variables I set in the IDE in normal runtime.

Just for Reference. I already did the ff.:


A.

@ClassRule
public final static EnvironmentVariables environmentVariables = new EnvironmentVariables().set("property1", "value1");

B.

@ContextConfiguration(locations = "classpath:application.properties")
public class LoginServiceTest {
...
}

C.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>  
      <environmentVariables>
        <SERVER_PORT>8082</SERVER_PORT>
      </environmentVariables>
    </configuration>
</plugin>

D.

@TestPropertySource(properties = {"property1=value1","property2=value2",})
public class LoginServiceTest {
...
}

E.

public class LoginServiceTest {    
static{
      System.setProperty("property1", "value1");
      System.setProperty("property2", "value2");            
...
}
iamjoshua :

UPDATE:

This solution worked on all of my test. You can refer here for more details.

@RunWith(SpringRunner.class)  //Add this
@SpringBootTest(classes = Application.class)   //Add this
public class LoginServiceTest {
...
}

Then on Run > Edit Configurations > Templates (in sidemenu) > JUnit ここでは、画像の説明を入力します。

ここでは、画像の説明を入力します。

  1. Then put all your environment variables here.
  2. Click + on the Upper Left window & Apply.
  3. Then delete all existing Unit Test command in your configuration and proceed to re-running all your test again plainly.
  4. Then you'll see the environment variables being added automatically on all your new and upcoming unit test.

    You won't have to manually add an environment variables to each unit test command alright as what I happen to do annoying before. Should you ever have an update on your environment variables. Delete the main unit test configuration you need to have an update, and update the JUnit Template instead so that changes will reflect to new unit tests that you may have.

PS:

Thing is, I still encountered the very same issue on some of my Unit Test, that even though the environment variables are being read properly on runtime and in some unit test alright with the above configurations. It is only during the other many Unit Test the very same environment variables are not being retrieved or received properly by the classes. At first I thought my classes are not really getting the proper environment variables and there must be something wrong as to how I stored the environment variables in IntelliJ, which it did not really, but it turns out this code format I have is the issue.

@Value("${fintech.clientid}")
private String clientId;
@Value("${fintech.secret}")
private String clientSecret;
@Value("${digifi-host}")
private String digifiHost;

It turns out,even though though this particular code works on runtime, it wont on Unit Test time, like ever.

私はすでにユニットテストに適切な環境変数を取得することができたまで、そこで私は、この特定のコードに手を加え。以下は私のために問題を修正したものと上記のコードの変更を参照してください。

@Value("${fintech.clientid}")
public void getClientId(String tempClientId) {
    clientId = tempClientId;
}
private  String clientId;

@Value("${fintech.secret}")
public void getClientSecret(String tempClientSecret) {
    clientSecret=tempClientSecret;
}
private  String clientSecret;

@Value("${digifi-host}")
public void getDigifiHost(String tempDigifiHost) {
    digifiHost=tempDigifiHost;
}
private  String digifiHost;

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=402434&siteId=1