Spring Test doesn't rollback REQUIRES_NEW, any workaround? - spring

To my surprise running JUnit test with #RunWith(SpringJUnit4ClassRunner.class) doesn't roll back REQUIRES_NEW transactions. The JIRA ticket doesn't get much attention.
Is there a work around to get Spring to properly handle REQUIRES_NEW transaction?

Related

Spring context initialisation hangs when executing a test annotated with #SpringBootTest after a test annotated with #DataJpaTest

I think I've stumbled about a bug in either Spring, Spring Boot, Spring Data JPA or Spring Data ReST. When I execute a test annotated with #DataJpaTest before a test annotated with #SpringBootTest the initialisation of the application context of the Spring Boot test hangs. Unfortunately the minimal example to reproduce the issue is too large to be posted here, so have uploaded an example project reproducing the issue here.
I have the following questions
Is it a bug or is it a strange setup?
Is the bug already known? (I have been looking for error problems in GitHub and StackOverflow, but found nothing that fits)
If it is a bug in which project Spring Framework, Spring Boot, Spring Data Jpa or Spring Data ReST should I open a bug issue?
Thanks for your help.

#DirtiesContext not working as expected after upgrading to Spring Boot 2.2.2

I am trying to upgrade my application from Spring Boot v2.1.8 to v2.2.2. However, after upgrading some tests start failing.
The pattern of failing tests strongly indicates that #DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD), applied on test class, is failing to clear context after each test case.
I am unable to narrow down further. Will appreciate any help.
I had the same issue today. With spring boot 2.2.7 not playing nice with Dirties context. In my case, I was using DBUnit to test and it wasn’t resetting the DB.
Adding this annotation fixed it for me:
#AutoConfigureTestDatabase(replace = Replace.ANY)

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)

Commit internal transactions while debugging JUnit tests in spring boot

I add the #Transactional annotation to my JUnit test class as suggested in the documentation.
However, the test methods are accessing multiple service methods, each being a transaction (and annotated as #Transactional too). Maybe these are not unit tests? anyway, I want my tests to call multiple service methods, no matter how you call them.
Internal transactions in the middle of the test are not committed to the database (because of the Test class #Transactional annotiation), so I can't check the DB while debugging the test after each internal transaction. However, if I remove the #Transactional annotation, the test breaks.
How should I configure the test so that it commits the internal transactions as they occur?
With Spring Framework 3.2.x you can use #Rollback(false) to instruct the Spring TestContext Framework to commit the transaction for your integration test.
Beginning with Spring Framework 4.2 you can use #Commit to achieve the same goal.

Spring boot with spring #Transactional works without enabling transactional management

In my spring boot application spring #Transactional annotation works without specifying #EnableTransactionManagement explicitly.
Is there any official documentation saying that it is enabled automatically?
Or there is something else happening .... ?
btw: I'm using Spring Data JPA
Yes, this is enabled as long as you have spring-tx and some transactional resource in your application. Effectively if you are using spring-boot-starter-jdbc or spring-boot-starter-data-jpa, Spring Boot will configure a DataSource for you, start Hibernate (in the latter case) and configure transaction management.
Not all "Enable" annotations require to be explicitly set. When there is a reasonable amount of things that we can check to validate it makes sense to configure that for you, we'll do it. In this case, if you have a DataSource you must probably want to have transactions. If you have JPA (and no JTA infrastructure), you probably want a JpaTransactionManager). If we auto-configure that, the easiest way to use it is via #Transactional so we'll enable that in that case as well.
I guess you kept asking to get some sort of "official" answer, so here's one.
#SpringBootApplication adds #EnableAutoConfiguration it detects Spring Data JPA on your classpath. According to it Spring registers PlatformTransactionManager - JpaTransactionManager, datasource, entitymanager, repositories.
Not sure there are precise articles, but there are proper answer on stack. An official spring sample article
#Transactional annotation can work fine if the "< tx:annotation-driven/ >" tag is in your Spring XML configuration. Look at this reference : http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/EnableTransactionManagement.html

Resources