loading initial data required by all the JUNIT tests and Rollback - spring

How can we load data which will be used by the JUNIT tests and at the end roll back using spring unit testing?

You can have your test class extend something like:
http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/test/context/junit4/AbstractTransactionalJUnit4SpringContextTests.html
See sections 9.3.2.3 and 9.3.5.4 the Spring documentation for further info:
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/testing.html#testing-tx
You can use this in conjunction with something like DBUnit to populate your database with known data.

Related

How can I have a clean databse for every JUnit test?

Problem: When an integration test fails, it often not delete his test data (I mostly use DbUnit for that). Because of this, other tests will also fail (for example because they want to delete their test data, but it is referenced by data which shouldn't exist at this point --> test will fail).
Is there a possibility that Jenkins use for every single test or for every Java test class a fresh/clean database with the init data (I don't need a complete empty database), that my integration tests are more independent?
Use a spring.jpa.hibernate.ddl-auto=create-drop property or annotate your test class with #Transactional.

How to write a proper test case for controller, service and Dao in spirng boot using junit 5?

How to write a proper test case for controller, service and Dao in spring boot using junit 5 with clear explanation
Spring Boot has a concept of test slices. This type of test configuration will setup only a part of your application and thus making tests:
less likely to break on not-related change,
faster in comparison to configuring all application services (using #SpringBootTest annotation).
For example #JsonTest slice will configure ObjectMapper (and some test utilities for JSON) in the same way as it would happen on production.
Anyway, to your mentioned types:
DAO - use #DataJpaTest slice - it will configure Hibernate with in-memory database and load all your entities and repositories.
Controllers - use #WebMvcTest(YourController.class) slice - it will load only configuration for Spring MVC, advices and your controller. You will be responsible to deal with dependencies of that controller.
Services - pretty much depends on what is your service doing. I prefer using slices also for services depending on Spring-configured beans but your test can also be a very simple standard [j]unit test with all dependencies mocked away. - Depending on the compromise you want to make.
This does not change with the fifth version of junit. The only difference is that you no longer need to annotate your tests with #RunWith(SpringRunner.class).

How to disable Javers for integration tests?

I am using Javers 3.11.2 with Spring Boot 1.5.17. When I am trying to run integration tests on an embedded database I still see that Javers tables are getting created each time.
Is there a way I can disable Javers during these tests so that these tables will not be created each time?
There is the easy way, put:
javers:
sqlSchemaManagementEnabled: false
in your application-test.yml. See https://javers.org/documentation/spring-boot-integration/
Disclaimer: I've never used Javers.
In general, disabling something in "integration tests" means that you don't want to load some beans (of Javers in this case).
This means in turn that you have to exclude them from the list of configurations spring boot works with.
If you're using javers autoconfiguration module, it has to provide in its own "spring.factories" file (can be found inside the jar) a file for autoconfiguration.
Find its java code and see whether it has some "#Conditional on something (property beans, etc.)" If it has than create a profile for integration test that will configure the beans in a way that conditional in javers won't pass and the bean won't be created as a consequence
If it doesn't have a conditional on something like this, you'll have to exclude the whole configuration. Its usually can be done by annotation #SpringBootApplication(exclude=<JaversAutoconfiguration goes here>
This will, however, turn it off also for production usage, which is obviously not something that you want. So for "production" profile, you'll have to import it as a regular configuration (not an autoconfiguration), for integration test profile you won't need this.

Is it good practice to have separate #SpringBootApplication for (junit) test and how

I have a Spring boot application initially setting on MySQL, so far so good. However now I am trying to create more unit test for JPA / DAO layer with H2 database.
I see several online demo that in Spring it is common practice to have an applicationContext-test for testing context setting.
Is it still good practice in Spring boot 1.4?
#SpringBootApplication(scanBasePackages = {...})
public class ApplicationTest extends SpringBootServeltIntializer{
....
}
As currently there is no separate xml file holding context for testing, is above looks like a good solution? And also is there performance impact that when the application starts all context for testing are also need to loaded in memory?
Also does that mean I need to create an application.properties in test sources? Spring boot has a lot of implicit process behind, but I cannot find much texts explain about the DAO layer setting for test in Spring Boot, so any guideline is appreciated.
My preference is to not use Spring for JUnit testing at all.
JUnit tests, by definition, should be about unit testing individual classes. Spring is a DI engine for satisfying dependencies. Using the real dependencies breaks the idea of a unit test; for those I manually inject mocks.
I do that to restrict the tests to individual classes. I find that creating the Spring factory and all the application beans takes a long time. I don't want to pay that price when I have a lot of unit tests. Keeping Spring out of the mix makes my tests run faster.

Spring Data Jpa. How to cleaning data from repositories befor run unit test from particular test classes?

I have problem with unit tests for persistance stuff written in spring data jpa.
For particular repositories I have a unit tests to be sure that everything works correctly. Also I have a integration tests. Each tests are passed when I run it for particular test classes. But when i run a whole package of tests I got a lot of faliures because I have records inserted into DB from previous tests.
Of course in each test classes I can add #After method to clean each data but I would like to ask that it posible to clean all data from DB before run tests from particular test classes without adding #After method?
Best Regards.
Use Spring's transactional test support to ensure that database changes are rolled back after each test:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html#testcontext-tx
One common issue in tests that access a real database is their effect
on the state of the persistence store. Even when you’re using a
development database, changes to the state may affect future tests.
Also, many operations — such as inserting or modifying persistent
data — cannot be performed (or verified) outside a transaction.
The TestContext framework addresses this issue. By default, the
framework will create and roll back a transaction for each test.

Resources