My Springboot REST API application uses RESTAssured integration tests with JUnit 5. I would like to be able to isolate these RESTAssured test so they only run against in memory H2 database and to do so, I use profiles like:
application.profile (which sets active profile to "development" or "production")
application-dev.profile (development profile using in memory H2 database for testing)
application-prod.profile (production profile using real database)
I am trying to isolate my RestAssured tests so that when I run maven > install, they will only run while I use "dev" profile.
If I use "production" profile, I would like not to run them as I dont want RestAssured tests to fire against real database.
I have my RestAssured test class annotated with #ActiveProfiles("dev") as:
#ActiveProfiles("dev")
class MyControllerRestAssuredTest { ... }
I would expect that if when my application.profile sets active profile like:
spring.profiles.active=dev, maven > install would run MyControlerRestAssuredTest
spring.profiles.active=prod, maven > install would NOT run MyControlerRestAssuredTest
However, regardless of how I set my active profile, maven > install will run the MyControlerRestAssuredTest tests.
What is that I am missing?
Try setting:
spring.profiles.active = dev
in application-test.properties
Related
I am currently doing karate testing, and while running my tests with active profile set to as "local" the application tries to boot up with active profile: "prd".
mvn spring-boot:run -Dspring-boot.run.profiles=local
When I run my application with the above command the active profile is correctly set to local.
mvn test -Dspring-boot.run.profiles=local -Dkarate.env=local -Pkarate
When I run my karate test with the above command, the application tries to boot up with active profile set to as "prd", which is the last available profile in the yaml file. When I change the order of profiles in the yaml file, still the last available profile is fetched during boot up.
I have written application using spring boot + scala with sbt and now I need to divide build configurations for dev and prod.
What has been done: created configs application.yml and application(-dev/prod).yml to start application locally, on dev and prod respectively.
What need to be done: find a way to configure spring boot profile (dev, prod) in javaopts or directly write corresponding config, also in javaopts.
I've tried to use these opts:
sbt service/run -Dspring.profiles.active=...
sbt service/run -Dspring.config.location=...
The answer is to configure active profile like this:
sbt service/run --spring.profiles.active=...
Is there any way to execute the environment variables file .env along with maven commands such as mvn clean install or mvn clean deploy. The main idea behind the concept that I'm looking for similar kind of solution:
mvn clean install -DenvFile=/path/<filename>.env
OR
mvn clean deploy -DenvFile=/path/<filename>.env
OR
mvn clean package -DenvFile=/path/<filename>.env
Note: Not trying to produce the environment specific builds. In dev environment, my intention to run the junit tests with all the
environment variables configured from <filename>.env.
where, the above maven commands should set all the environment variables from <filename>.env and then execute the maven plugins. In IntelliJ, there's a envFile plugin which exactly do the same.
Don't want to have environment specific properties dev|staging|prod.properties in my project because it's messy and hard to manage. I'd rather prefer to have one single environment specific file filename.env which contains all the dynamic/changeable properties.
application.properties
spring:
cloud:
config:
uri: http://config-service:${CONFIG_SERVICE_PORT}
fail-fast: true
password: ${CONFIG_SERVICE_PASSWORD}
username: user
Environment File: .env
CONFIG_SERVICE_PORT=8080
CONFIG_SERVICE_PASSWORD=123
Now when I deploy the application in different environments like AWS, GCP and Azure. All I need to change the environment variables in the .env file and run the application java -DenvFile=/path/<fileName>.env -jar application.jar and it will do the magic.
My problem is related with maven-sure-fire plugin for testing in dev-mode, which require these environments variables for spring context.
Any help would be appreciated.
Ok from your comments it seems like you're looks for two different solutions:
Run the application in different environments with java -DenvFile=/path/<fileName>.env -jar application.jar
Solution for running tests.
These are different issues I'll try to address both
Issue 1
When you run java -jar this means that the artifact is already assembled (with the help of spring boot maven plugin as far as I understand).
This jar is a ready to go spring boot application and maven is basically irrelevant here - maven is a build system, spring is a runtime framework and we're talking about the runtime.
Spring boot has a lot of ways to achieve what you want. A "Native" spring boot way which is close to your situation is running the application with "--spring.config.location=file:// with all the required configurations
It looks like this (see here for complete documentation):
java -jar application.jar --spring.config.location=myprops.properties
Even if you have some properties defined in src/main/resources/application.properties this method allows to override them effectively providing a way to run different configurations in different environments.
This has an advantage over the .env files because it can run in the same way in all the OS-es, even Windows ;) Since Java is OS independent - I believe its the best you can achieve.
Of course you can wrap the java -jar line in some kind of bash script and load / execute a series of export commands before running the jvm, but again, its less "spring-y" way.
Issue 2
Maven runs the tests (unit/integration) in a way that spring boot maven plugin is irrelevant. Its all about surefire/failsafe and spring boot testing framework.
I'll assume you're asking about integration tests because I believe this is all irrelevant for unit tests, since those should not require any environment variables at all and should be run without spring at all (junit/mockito should do the job)
I'll also allow myself to keep the assumption that the way of overriding/configuring the spring boot application via yaml or properties file is better than .env and will provide spring test configuration solution here:
With these assumptions you can create a yaml file in the following path: src/test/resources/application-test.yml
This file can contain configurations relevant for tests and will override anything written in src/main/resources/application.yml. Note, since application-test.yml resides in test sources, spring boot maven plugin won't package it into the application.
Depending on the exact way of doing integration tests you might consider also using #TestPropertySource annotation to provide the custom properties/yaml file that doesn't follow spring boot's default convention. Its especially useful for spring driven tests that do not bootstrap with the spring boot full fledged support (read the tests that use junit's spring runner but don't have annotation #SpringBootTest)
Another possibly useful annotation is #ActiveProfile("myprofile"). This will cause spring boot tests to automatically load file src/test/resources/application-myprofile.yml (or application-myprofile.properties)
Last but not least I'll refer the second comment with "dev/prod/staging/properties" in the source.
When it comes to tests - there should be only one file application-test.yml. However note that when you're using yaml, its possible to define configurations for many spring boot profiles in the same file:
# default value
foo:
bar: 1
---
spring:
profiles: staging
foo:
bar: 2
---
spring:
profiles: prod
foo:
bar: 3
Some relevant SO thread
IntelliJ has a nice integration with JUnit that allows me to run some or all of my unit tests and visualize the results of each test and test class with a collapsible/expandable hierarchy that even takes into account JUnit5 nested test classes. I can click on any failed test to see the log output from running just that method. It looks like this:
I have now defined an integration test phase to my project using two popular Maven plugins:
maven-failsafe-plugin separates my JUnit tests into two phases. The test classes ending in "Test" run during the test phase, and those ending in "IT" run during the integration-test phase.
fabric8's docker-maven-plugin which I'm using to spin up a test database in the pre-integration-test phase and shut it down in the post-integration-test phase.
I can tell IntelliJ to run a Maven command (such as maven clean verify) that runs this lifecycle and tells me if the tests have passed. However, a Maven "run configuration" in IntelliJ doesn't produce the same helpful output. All I get is the success or failure status of each phase, and I can navigate to the console output for a phase, but not an individual test. The output looks like this:
So here's my question: Is there any way to configure IntelliJ (or Maven, or both) to give that hierarchical test-by-test output while keeping the Maven lifecycle I've defined, with the temporary use of a docker-container database for my integration tests?
I know that I can manually spin up the test database container, use IntelliJ's JUnit runner for my integration tests, and then manually stop the container. Can I do better?
Unfortunately there is no tool to show test-by-test output while keeping the Maven lifecycle as Maven run configuration is used in this case. Please use Junit run configuration for Class/method output in test runner tab.
I am having a Spring Boot application with around ~500 Tests (Unit and mostly Integration).
When i start running the tests in IntelliJ with second click -> Run all Tests - tests run in around 10-15mins.
When i try to run the tests with "mvn verify -P itest" in IntelliJ terminal, the execution time is around an hour. This is because this command starts and stops the Spring Test Runner Server on every class (which is not my desired result).
The IntelliJ second click -> Run all Tests starts it only once.
Our Jenkins Job is running the tests with "mvn verify -P itest", so my question is how can i change this behavior, to start and stop the Test Runner only once (with this "mvn verify -P itest" command).
itest is the profile that we have in our pom.xml which includes the classes that contain integration tests (using maven failsafe plugin)
Here are the annotations of our base abstract Integration Test class
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(classes = MainApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
I found this article which could by similar to my problem, but this does not explain the behavior that IntelliJ gives me.
Reset Spring-Boot During Integration Tests
Thanks a lot in advance
I found out why this was happening. There was a setting in my pom file which stated
'reuseForks' maven failsafe property to be false.
I changed it to be true and now it is working fine.
Intellij works in the same way - it is reusing the forks for all the tests.
Read More
http://maven.apache.org/surefire-archives/surefire-2.17/maven-failsafe-plugin/examples/fork-options-and-parallel-execution.html