Maven Verify Starting and Stopping the Spring Test Runner on Each class - maven

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

Related

How to visualize output of JUnit integration tests in IntelliJ when using docker-maven-plugin?

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.

Debugger, #SpringBootTest, and Gradle

I'm new to Spring Boot and Gradle and cannot figure out how to suspend JUnit tests and connect to them with a debugger.
I invoke the automated tests on the command line gradlew build. What I'd like is so the execution to pause wen it reaches the test task and wait for a debugger to connect.
The JUnit test classes are annotated like this:
#RunWith(SpringRunner.class)
#SpringBootTest
#ActiveProfiles("test")
#AutoConfigureMockMvc
I have tried adding the JVM arguments for debugging to the gradle.properties file and I still can't get it to suspend, much less listen on a port for the debugger. I'm not sure what information to provide you -- ask me in the comments for files, code, or settings.
Gradle 5.5
Spring Boot 2.16
Java 11
#Omid. Thank you for the link. The solution was simple.
UPDATE: Do not use gradlew build. Use the test task instead.
gradlew test --debug-jvm
In the Gradle 5.5.1 docs:
https://docs.gradle.org/current/userguide/java_testing.html#sec:debugging_java_tests

Running selenium unit tests with Spring Boot

I have some integration tests that use Selenium and the HtmlUnitDriver to verify my web app behaves correctly from the browser. In IntelliJ, I'm able to run ./gradlew bootRun to start my embedded web server, and then run my Selenium tests manually.
The tests run as expected.
However, I'm wondering what the best strategy is to run these in an automated fashion on my CI Server (TeamCity in this case). Simply running bootRun doesn't quite work since the task runs until it gets terminated.
Should I create a script that runs bootRun, and then I can terminate gradle somehow after the tests complete? I'd also like to use my application.properties file I have in src/test/resources instead of src/main/resources which bootRun uses normally.

Configure cucumber in TeamCity to run unit tests only using tags (Maven)

I have both unit and acceptance tests in a maven project (spring boot). I would like to run unit tests only when my build runs in TeamCity. I am using cucumber.
When i run the tests via command line, everything works as expected (only unit tests are run)
c:\apache-maven-3.3.9\bin\mvn package -Dcucumber.options="--tags #unit"
However, in teamcity, all tests are being run (unit and acceptance). It seems teamcity ignores my cucumber.options
In addition, when I double click on the 'test' lifecycle in Intellij, all tests are run as well (not just unit tests) So my guess is that TeamCity is doing exactly what the 'test' lifecycle does.
How can i get around this problem (in TeamCity)?. I have tried using a 'Command line' step, which works, however, i lose all the tests reporting as well as test coverage reports.
i have solved the Intellij problem by creating (or changing) a configuration:
Try writing it this way in TeamCity:
"-Dcucumber.options= --tags #unit"

IntelliJ runs unit tests with Maven instead of JUnit

I have some Unit tests, that when I try to run, it automatically creates Maven run/debug configuration, instead of JUnit (the integrated IDEA tab).
For some tests it does use JUnit run\debug configuration and manually - I can create both Maven and JUnit.
How do I make JUnit to be the default test runner ?
The problems is that I had maven runner plugin installed, causing all my tests to run with Maven
You can use the maven option "-Dskip=true" to suppress the maven test execution, and you can add your own test configuration to the build process. This screenshot was taken form IntelliJ 15.0.2.
Update: maven option

Resources