I have a java - maven -project with multiple modules.
In each module, I have mock tests and tests using database.
Mock tests names end with "Test" and database tests end with "IT".
UserTest and UserIT for example, are in the same package and in the same module.
I'd like to run first all mock tests, then all database tests.
is there a way to do this ?
Thanks.
Related
I have some unit test classes in my project that I want to test with mvn test. But whenever I run the command an instance of spring boot starts and opens database connections.
I´m running these tests in a server that doesn´t have access to a database, and none of my tests uses one. I just one to execute the tests as the IDE does.
I think one of them could help you
1 - Disable all tests
mvn clean install -DskipTests
2 - Prepare your test for switching
#ExtendWith(SpringExtension.class)
#EnabledIf(expression = "${tests.spring.enabled:true}")
if you pass -Dtests.spring.enabled= false or true will disable test
3 - Point your test to other configuration
#ExtendWith(SpringExtension.class)
#TestPropertySource(locations = "classpath:application-${spring.profiles.active:test}.properties")
this way you could connect your application into a local database
4 - Change your tests
Use mocks to turn your integration tests into a unit tests or component tests with no connections
The problem I had was an empty class with #SpringBootTest annotation. After deleting it the tests works without spring interfering.
I'm working on a multi-module maven project using Java 8, Spring Boot 2.4.0. I want to test one of a module that is calling a 3rd party service. I'm using wiremock to mock that 3rd party service call and have created a spring boot integration test class. My class is in the same package where my XYZService class is. My test is in src/test/... and looks like this.
#RunWith(SpringRunner.class)
#SpringBootTest(classes = SpringApplicationClassWithMainMethod.class)
public class XYZServiceIntegrationTest {
#Rule
public WireMockRule externalService = new WireMockRule();
#Test
public void test1() {...}
#Test
public void test2() {...}
}
When I run the maven build in my eclipse (clean + install). My build is not detecting the tests present at the maven-module where the Integration test is located. The other unit tests in the same module are also not being detected (Note: before adding the integration test class, the unit tests were working). Maven says Tests ran: 0. The integration test is working fine when I Right click on the file and run as JUnit test(Junit 4). Also, I have some environment variables that need to be set for running the SpringApplicationClassWithMainMethod.class that I'm setting within the configurations of that Integration test class in order to successfully load the Application context(I tried to load the environment variables through code and nothing from other stack-overflow posts worked). One more thing to inform my maven only uses Maven surefire plugin for running tests. I dont know if we need to have Maven fail safe plugin for my purpose(Is my test considered a Integration Test when I added the 2 annotations on top of the test class?). Can someone please help me with any suggestions on how to build the parent project.
Adding the maven-failsafe-plugin into my child pom worked. In the parent pom I added the execution goals for maven-failsafe-plugin in the and in the child pom I inherited that plugin.
While upgrading to JUnit 5 (version 5.5.2), I have made a strange discovery with the suite functionality: my suites can find and run tests that end with the word "Test" but fail to find tests that don't end in "Test" (in my case, they end in "Base").
In JUnit 4, we used the #Suite.SuiteClasses() annotation to find these tests, but the JUnit 5 #SelectClasses annotation seems to miss these test classes entirely. Even using #IncludeClassNamePatterns({"^Com.*Base.*?$"}) fails to detect the tests, which I found strange (the tests I want to run start with "Com"). After this, I tried the #Tag() annotation, which didn't work either.
I assumed this was because Maven Surefire (version 2.22.2) only detects test classes that start with Test, or end with Test, Tests, or TestCase. So, I tried to include my Base test case:
<includes>
<include>**/*Base.java</include>
<include>**/Test*.java</include>
<include>**/*Test.java</include>
<include>**/*Tests.java</include>
<include>**/*TestCase.java</include>
</includes>
Maven was able to run these Base tests when I built this project, but the test suites still failed to find them.
My code will look similar to the following:
#RunWith(JUnitPlatform.class)
#SelectClasses({
Com_TestOne_Base.class,
Com_TestTwo_Base.class,
Com_TestThree_Base.class,
Com_TestFour_Base.class,
Com_TestFive_Base.class,
Com_TestSix_Base.class,
})
public class Com_Base_Suite {
}
The result of running this suite is a success, but no tests actually run. All these tests have been updated to JUnit 5 and run successfully on their own.
The problem you're running into results from mixing up JUnit 4 and 5. Maven Surefire is able to run JUnit 5 (aka JUnit platform) tests out of the box - given you have the right dependencies in your pom. See e.g. https://github.com/junit-team/junit5-samples/tree/master/junit5-jupiter-starter-maven for a minimal pom.xml.
JUnitPlatform, SelectClasses et al. allow you to run JUnit platform tests through JUnit 4. You'd probably only want to do that if your build tool or IDE do not support the JUnit platform themselves. JUnit 5 does currently not have any explicit support for test suites similar to JUnit 4's #Suite annotation.
I recommend you get rid of Com_Base_Suite alltogether and go with a naming convention that can be configured through Maven's <includes> section.
How do I configure Arquillian Suite extesion?
https://github.com/it-crowd/arquillian-suite-extension
I would like to use it for single deployment tests, in order not to have to deploy for every single class that have #Test methods in my project.
By the way, I'm using TESTNG with arquillian..
I pushed extension bit further, it can be found on maven central and there is part of help written + tests to look how it should be done.
I also created "generic" deployer builder that should work with javaee6.
https://github.com/ingwarsw/arquillian-suite-extension
I'm using maven war plugin to build war package.
Before package is build test are executed. To preinitialize my database with sample data I use spring bean. I would like to have different data in my db for tests and different when application starts.
I was thinking that maybe it is possible to use two different spring initializer classes in 'test' and 'war' phases but I don't know how to achieve this.
You have to put the different classes you need into src/main/java or src/test/java or may be supplemental application.xml into src/main/resources or src/test/resources. The test initializer can be done by a Test class which initializes first before all tests are running (take a look at testng which has this kind of feature).
Your tests should not be using the production Spring context (xml) files.
Instead, if you need to access an ApplicationContext in your tests (or if you are using a base testcase class like AbstractTransactionalJUnit4SpringContextTests), set up a test-context.xml context which points to the test database configuration and the test data scripts.