How to break integration test when Spring context initializaiton fails? - spring

All my tests share the same Spring configuration. It works effectively because context is started once and reused. Whole test suite completes in short time.
The problem is when Spring configuration is broken. In such case first test fails with
IllegalStateException: Failed to load ApplicationContext
and each next test the same. It last long, because for each test context is started and failed.
Usually I am satisfied when testing process is continued after failure. But in this case I prefer to stop testing process - if context is not started once, it will never start successfully.
Is is possible to break test process in such case in JUnit 4/5?

JUnit 4 has option to request stop tests, see https://stackoverflow.com/a/31397394/2365727
But using it is a bit difficult.
JUnit 5 seems do not have equivalent

Related

Embedded Kafka test cases not considered in test case execution

I have added EmbeddedKafka for testing purpose and introduced 3 more Kafka test cases. Total 8 test cases.
When I am running test cases from intellij all test cases are running as expected (all green) 8/8
While executing maven install or test command Kafka based test cases not considered as consumer stopped so just 5(non-kafka bases) test cases executing.
Code and logs added in below link
https://github.com/kasramp/spring-kafka-test/issues/2
Could you please assist on this ?

#SpringBootTest loads application context and runs indefinitely without running test case code

I'm trying to develop library and write integration tests using #SpringBootTest.
I'm supplying custom #SpringBootApplication class and when i trigger tests, testcase will start running, spring context loads(banner, hibernete logs) and stucks forever. it doesn't come back to test case code to run it. I've enabled debug logs but nothing useful found. not sure where it's going wrong

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.

Camel integration spring test without #DirtiesContext

When I run an integration test of camel route processing as a standalone test, it passes. When I run all the tests, it fails with assertion errors of expected values. When I add #DirtiesContext to the abstract test class, they all pass ok.
I checked the docs and beyond the paragraph below, they do not say why #DirtiesContext is actually needed, and what goes wrong when not used.
Notice that we use #DirtiesContext on the test methods to force Spring Testing to automatically reload the CamelContext after each test method - this ensures that the tests don't clash with each other (e.g. one test method sending to an endpoint that is then reused in another test method).
The thing is that creating spring context again and again is quite time consuming. All our other non-camel integration tests pass without #DirtiesContext (using #Transactional) and we would like to continue with that.
Is there a way to put camel to pristine state without recreating the whole spring context so the test's MockEndpoints work as expected?

jpa problems running multiple tests with spring

my problem is that when I run tests individually the test work properly but when I run all the tests several tests start to fail.
It looks to be a problem of the applicationContext I have 3 applicationContext but the applicationContext-test import the 2 before.
Some times I do
#applicationContext(applicatonContext1.xml)
and other times
#applicationContext(applicationContext-test.xml)
I get different exceptions for different tests:
SQLGrammarException: could not execute query.
Detached entity exceptions
ConstraintViolation exception.
Some tests work properly and some no, but I don't know why.
Thanks
PutDirtiesContext on test to allow context to be reloaded each time you run test.
Different #ApplicationContext should work fine. I think you not clean the database in your test (because without #DirtiesContext spring will use same context (and same DB) for same #ApplicationContext).

Resources