Run Spring Boot application with test application.properties - spring-boot

I've just installed Cypress to run e2e tests and that requires my Spring Boot app running so it can plug into the browser to run tests.
I already have an application.properties file under test/resources which creates an H2 memory database populated from a test/resources/data.sql to run my unit and integration tests.
I want to start my app using those test resources so I can run Cypress tests with my testing environment, is that poosible?
I tried changing SPRING_PROFILES_ACTIVE env variable to test but it doesn't take my config files under the test folder, what can I do?

test/resources/application.properties is, as far as I understand it, purely for use in (unit/integration/service) tests that live in test/.
Try creating an additional application-test.properties file in main/resources. This should be used when you have the test Spring profile activated (although it might be better to choose another name for the profile to avoid confusion).

Related

How to create spring boot test suite

Say I have 10 spring boot test class (annotated with #RunWith(SpringRunner.class) and #SpringBootTest)
Each test needs to launch spring container for like 10 seconds, although the container might do the same init.
So I may need 100 seconds for "mvn test".
Is there a way I can group my 10 test class into 1 suite, and let the container only start once.
So I can:
Only run the suite for "mvn test". (with proper naming for individual test class)
Optionally run individual test in IDE.
Spring uses Cache Management to cache the Application Context between tests:
By default, once loaded, the configured ApplicationContext is reused for each test. Thus, the setup cost is incurred only once per test suite, and subsequent test execution is much faster. In this context, the term “test suite” means all tests run in the same JVM — for example, all tests run from an Ant, Maven, or Gradle build for a given project or module. In the unlikely case that a test corrupts the application context and requires reloading (for example, by modifying a bean definition or the state of an application object) the TestContext framework can be configured to reload the configuration and rebuild the application context before executing the next test. (https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/testing.html#integration-testing)
So this mechanism tries to execute your integration tests on an already running Application Context if possible. As you see multiple Application Context launches, this indicates your tests somehow use a different setup e.g. different profiles active, test properties, MockBeans etc.
The Spring documentation provides an overview on which indicators it puts an Application Context in its cache: https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/testing.html#testcontext-ctx-management-caching
If you e.g. don't change any test property for your integration tests, Spring can run all of them on only one Application Context and be extremely efficient.
Another indicator for your current behaviour might be the use of #DirtiesContext which leads to a fresh Application Context after your test executes.

How to ignore a java file when maven install

I have a #Service class where i'm caching some table data. I don't want those queries to run while building mvn install. Is there a way to ignore the file while building and it only execute when i start the server ?
It's a spring-boot application.
Here is background of my issue. I have initialized the spring boot app from http://start.spring.io/ site, which actually adds dummy application test file with SpringBootTest annotation and default contextLoads() with Test annotation, with an intention to initialize and execute all test cases, which needs to initialize and execute all code base. In my opinion this is not required, as we can have respective Test classes per controller/manager, which will give more controlled environment to hook up your Test setups and executions.
I have removed the default application Test file and included respective test classes for code coverage and quality. This way my beans are not executed at server startup time rather build time.

Can you get Spring Boot JUnit tests to use the same server?

I have some Spring Boot JUnit tests that require a somewhat lengthy server start up (I'm loading a complex domain in JPA). I've put them into a test suite, but each test kicks off a new server start up.
Is it possible to set them up in such a way that the server is only started once and each test is loaded onto it and run as if the server were started by the test itself?
Okay, so the solution here is actually built in to Spring testing. That is, it caches ApplicationContexts for tests, as described here, as long as the various things like properties are the same.
Ironically, I screwed this up by trying to speed up the tests by using test properties to limit what was loaded.

Multiple Profiles For Spring Integration Tests?

I need different profiles for a few things. First we have the issue of my databases. When I run local tests I expect to use one datasource. When I run an acceptance profile (for a CI acceptance build), I expect to use a different datasource. Finally, when I run acceptance not in test I expect to use a third datasource. How I imagined this would work is.
/src/main/resources/application.properties
/src/main/resources/application-acceptance.properties
/src/test/resources/application-test.properties
/src/test/resources/application-acceptance-test.properties
However when I run mvn clean install -Dspring.profiles.active=acceptance it does not run the application-accepatnce-test.properties.
Finally, I would like to be able to run a mvn install while running the tests but not the integrations test. For this I imagine I would add a -Dspring.profiles.active=nointegration and then simply add an #ActiveProfiles('!nointegration') on the integration tests.
I've had no luck with either of these. Is it even possible to get profiles on test runs?
If it helps I am using Spring Boot 1.3.0.RELEASE.
EDIT:
On my integration tests I have #ActiveProfiles("test"). Is there any way to generate the profile here based on the java-opt spring.profiles.active?

Arquillian: how to run a test without packaging/deploy/start/stop tomcat

I'm wrinting a junit test with #RunWith(Arquillian.class) annotation like described in https://docs.jboss.org/author/display/ARQ/Drone
While writing the test I would like to rin it without wait package war, start tomcat, deploy war, stop tomcat each time.
I run test inside eclipse and I can run tomcat with my web application once and run test multiple times inside the IDE.
Is there any parameter to let Arquillian use an already deployed and running application, without change the source of my test class?
Nope.
Arquillian is about creating deployable archive and testing it inside true server container.
By the way: if you are looking for ways to speed up your development, then take look at tomcat remote adapter. Generally with remote adapter you don't have server startup for each test launch. Just start it manually once.

Resources