DataJPATest attempts to load everything - spring-boot

I have my application, with a lot of components.
I'd like to test my queries so I annotated my tests with:
#DataJPATest
Problem is that it also tries to load every single bean!
What am I missing?

Related

Difference Between Spring MockMvcBuilders.standaloneSetup and WebMvcTest

I have a Spring Boot 2.6.0 application that depends on libraries which use fields initialised with SpEL syntax, e.g.
#Value("${some.value.in.library}") aField;
I attempted to write a Spring MVC test for a controller with #WebMvcTest. However I kept getting failed to load applicationcontext errors due to a SpELEvaluationException for the fields mentioned above.
Even after trying many, many, different test annotations such as #ConfigurationProperties and other fixes. I could see the properties that are defined in application-test.yml being loaded in the startup logs but they were not getting injected into the class.
Finally, I tried using:
MockMvcBuilders.standaloneSetup
and the test was able to start and run successfully.
Unfortunately, due to the complexity of the application and the use of a library where the problem occurs, it is difficult to create a minimal reproducable example.
So my question is, why does MockMvcBuilders.standaloneSetup work, whereas #MockMvcTest doesn't?
This answer suggests that #MockMvcTest should not load the full application context. But that does not seem to be the case based on the failed to load applicationcontext errors.

start the Spring Boot application once, before Cucumber tests run

I am writing some BDD tests using Cucumber for my Spring Boot application (v2.2.1), and it works OK.
However, I am facing some performance issue, because the application gets started/stopped for every scenario in the feature file : I am using in-memory DB with Liquibase, so for each scenario, this gets executed (takes a few seconds).
Sure, it's currently guaranteed that my scenarios are very well isolated.. Maybe in some cases I will want this behaviour, but right now, most of my feature files would benefit from a one time set up : since each scenario sets up different records (with no overlap) it needs in the in-memory DB, I could theoretically executed my scenarios in parallel on a single Spring Boot application running.
I saw https://blog.codecentric.de/en/2017/02/integration-testing-strategies-spring-boot-microservices-part-2/ , but it requires to have built the application first, then start it from the jar.
Isn't there a way to do the same, but with the application started once from the Cucumber runner ? any example somewhere ?
Thanks to #mpkorstanje link, I was able to find the issue : while trying to replicate the suggestion in my project, I discovered that one of the config that was scanned had a #DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) annotation.. So that was the issue. Now I need to look at a workaround like what is suggested here : #DirtiesContext tears context down after every cucumber test scenario, not class

How to properly cleanup/close database after test class

I've problem with spring boot integration test. I have aproximately 300 tests and during their execution more than 150 postgres connections are opened.
Tests are annotated almost everywhere by SpringRunner. Sometimes there is more annotations.
This also causes that the test are executing without the end probably.
In my opinion database is not removed with context change but have no idea how could I invoke this process.

Spring transactional tests - prepare data in BeforeClass

I would like to create some data in #BeforeClass and use them in my #Test method.
After all tests are finished (failed or succeeded), I would like to rollback the data.
Can this be achieved using the annotations #BeforeClass, #Transactional, without having to clean up the data explicitly?
We had a similar problem here. Our solution was to give each test class an embedded H2 database (just takes a couple of seconds to setup the first one; after that, it's not really noticeable anymore).
That allowed us to load any kind of test data into the database, no need to clean that up after all tests have run.
Each test still gets its own transaction, so after each individual test, the per-class database is rolled back to the original state.
To debug tests, we would annotate the individual test with #Transactional(rollback=false), so we could look at the database with an SQL tool.
Another test would examine all tests classes, looking for this annotation to make sure no one accidentally commits it.

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