0

I'm working on multi-module maven project using Spring Boot 2.4.0. I have written integration tests for a module. The test class looks similar to this.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringApplicationClassWithMainMethod.class)
public class XYZServiceIT {

@Test
public void test1() {...}

@Test
public void test2() {...}
}

To run the SpringApplicationClassWithMainMethod.class i.e., in order to load the Application context I need few environment variables that I set in eclipse. So, in order to run the above integration test while loading the SpringApplicationClassWithMainMethod.class I need those environment variables before the Application context is loaded.

Trial-1: I have tried using @TestPropertySource(properties = {"key1=val1", "key2=val2"}) annotation, that didn't work.

Trial-2: I have also tried the static block to set environment variables which didnt work.

Trial-3: I also tried using the @ContextConfiguration with a ApplicationContextInitializer class, that didnt work as well.

All these attempts to build the project using maven only lead to a

IllegalState Failed to load ApplicationContext

error for the above test class. Is there any way I could load the environment variables before the Application context gets loaded?

justAnotherDev
  • 223
  • 1
  • 6
  • 15

1 Answers1

0

I think the correct class naming convention for Integration tests in maven would be XYZServiceIT since *Test is reserved for unit tests already which are run before application context. You can change that if needed in your maven pom or simply stick with the conventional naming.

UPDATE

To pass environment variables to maven for your integration test use the following:

  1. make sure you installed M2E from eclipse marketplace (found in menu > help > eclipse marketplace)

m2e plugin from marketplace

  1. right click your project > Run As ... > 4 Maven Build ... PS: afterwards you can find your run configuration at the top underneath the dropdown of the green arrow and in the run configuration settings if you need to rerun the tests in the future

first maven build from rightclick on project

  1. configure maven environment parameters either inline (for the maven goal command) with verify -Dkey=val or in the bottom variable section. both work for unit and integrationtest. environment typically does NOT work for test stage. (If you don't have an JDK as the runner you will get an error. Follow this post to fix it if needed: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?)

mvn goal settings with environment vars

I hope that helps. You can also change pom.xml profiles if needed but i wouldn't recommend that.

zeg
  • 432
  • 2
  • 9
  • I actually have the class as XYZServiceIT and its working. The problem is I'm unable to load the application context. I need to have the environment variables loaded before the application context is loaded. – justAnotherDev Mar 04 '21 at 21:19
  • I don't know which environment variables you are missing. Do you enter them in java commandline with `-Dkey=value` or inside the code with `System.setProperty` or something entirely different like spring variables from a config file? – zeg Mar 04 '21 at 21:21
  • I have set few variables in my eclipse. For example, When I run the SpringApplicationClassWithMainMethod.class I have set few environment variables by Right click > Configurations > Environment. I need these variables any time I want to run the SpringApplicationClassWithMainMethod class. Since I'm doing an integration test using that class. I need those environment variables to be loaded when the maven performs those integration tests – justAnotherDev Mar 04 '21 at 22:01
  • You propably set them for the "Run Configurations" in the Java Application section. This way only `java` will get them during normal application runs. Maven usually uses another command, the maven build or to be more accurate `mvn verify` in your case. You need to set the environment variables in the Maven Build section aswell i guess. – zeg Mar 04 '21 at 22:10
  • How can I set environment variables in Maven? Can you give me an example, lets say I have 2 environment variables say, key1=val1, key2=val2 How can I set these in maven? – justAnotherDev Mar 04 '21 at 22:41
  • In eclipse Right Click, Run As, Run Configurations and inside the Maven Section. Just as you did for the Java Application Section i think. If you use a maven builder plugin like M2E pass them to the command like this `mvn clean build verify -Dkey1=val1 -Dkey2=val2` before applying the command – zeg Mar 04 '21 at 22:47
  • i updated the answer. If that fixs your problem i would be happy if you would accept the solution :) – zeg Mar 11 '21 at 11:39
  • This would work in my system but it will fail during the jenkins build. I was able to fix the issue by adding the environment variables in the pom.xml file of the parent project pom file. Can you add this to your answer above. – justAnotherDev Mar 12 '21 at 17:48