Spring context cache between integration tests - slow startup times - spring

I have a spring boot application and when I am doing some integration testing I must wait the spring context to load, for example it takes about 4 seconds for the JPA slice (I know this is not too much time). Is there a way to make this instant at least while I am developing?
e.g. something like spring-dev-tools but between integration test runs, or run a separate integration testing application, or lazy init whole spring boot context

Related

How to prevent cucumber from hatching several SpringBoot context

I am using Cucumber + Spring Boot + Spring Cache
Every time Cucumber jumps from one Scenario Outline to the other, my Spring Boot context is rebuilt (I can see the Banner) and my Spring Cache is rebuild... which takes a lot of time.
I tried to add #DirtiesContext above the #SpringBootTest(Web) on the java steps class, that didn't helped.
How can I prevent Cucumber from refreshing the context on each and every Scenario?
Thanks
Upgrade your Cucumber to v5 and make sure you do not have #DirtiesContext any where in your spring configuration.
https://github.com/cucumber/cucumber-jvm/tree/master/spring

Not able to write Test cases for Spring Boot application with actual DB connection from Service to DAO

Can anyone let me know, how to write JUnit test cases for Spring Boot application with actual DB connection?
I mean to say, when we right click on #Test class in src/test/java, and click on Run as JUnit Test, we need to Autowire all the beans of Service and DAO which we had developed in src/main/java and control should flow from #Test class to Service and Service to DAO and queries should be executed using #PersistenceContext Entitymanager and return successfully with the desired results.
The stack specifications
Spring Boot 1.5.10
JPA
Please help me...
You can first refer to the documentation of the SpringBootTest
Spring Boot testing instruments allow you to 'slice' application into pieces, test it separately and test application as a whole. If you want to concentrate on database testing - consider using #DataJpaTest.
As for databases: it is a more common case to use in-memory databases like H2 during testing. But, if you want to test against the real databases, take a look at TestContainers or it's particular implementation (test container spring boot)

spring application start up time

Spring application war file takes more than 15 min to deploy in tomcat server. Our initial finding is that nested dependency of huge number of spring components is attributing to this delay.
Predominantly it is the repository initialization which takes time. Is it possible to lazy load the spring repositories or trigger compilation of named queries before deployment ?

Rollback Integration Test which uses cucumber and tests the application on embedded tomcat via maven

I am trying to set up an integration test framework for my spring project using cucumber framework and selenium for UI automation.
What I have done till now is
Use tomcat7 plugin for maven and deploy my war file to this embedded tomcat
Use the cucumber feature file to specify any test cases, be it a rest service or for the UI automation
We need to use our existing development DB for test purposes
I will hit the app url for the application running on the embedded tomcat.
What I am unable to find out is, can we rollback a transaction automatically after the test is done?
I am able to rollback transactions from cucumber/junit that were directly using my DAO as the DB connection was initiated from the same spring context.
But when the invocations are made using this model, I am unable to find any idea to rollback the transactions after the tests are done.
Can you think of achieving your integration testing in terms of Spring profile? You can create a spring profile for integration test and use it as environment variable or using annotations to specify rollback for transaction.
Take a look at following references to get an idea:
Spring integration tests with profile
https://spring.io/blog/2011/06/21/spring-3-1-m2-testing-with-configuration-classes-and-profiles

Using Spring Boot Configuration in a custom JUnit test runner that does not otherwise use Spring

I have a custom JUnit test runner that executes acceptance-level tests using a test specification format specific to my project. The system under test is using Spring Boot and takes advantage of its configuration facility. I'd like the tests to be able to read the same configuration files in the same way. Obviously, using Spring Boot Configuration itself is an answer.
I'd like to just use Spring Boot Configuration as a stand-alone library, but I'm willing to fire up Spring Boot if that's what it takes. I'm not in control of the top-level application - JUnit is. So, I don't know how to start Spring Boot when I get control inside my test runner.
I've looked at extending SpringJunit4ClassRunner but I can't keep it from looking for #Test annotations and failing when it doesn't find any. I've started to look into merging code from SpringJunit4ClassRunner into my custom runner. Before I go too far down that path, I'd appreciate input from the community.
It sounds like you simply want the application running for a standalone webservice testing. This can be done simply by scripting the "java -jar" command to run the spring boot application. However, I would question why you don't want to leverage the testing tools built into spring boot? You can fire up the entire spring boot application and write some very logical looking tests.
For example a rest api test case:
#Test
public void homePage() throwsException () {
mockMvc.perform(get("/readingList"))
.andExpect(status().isOk())
.andExpect(view().name("readingList"))
.andExpect((model().attribute("books", is(empty()))));
}

Resources