Commit internal transactions while debugging JUnit tests in spring boot - 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.

Related

How to automate the keycloak setup while running Junit test cases?

I have some Junit test cases in my Spring Boot project. I am using keycloak for getting tokens on the basis of the username and password in order to perform authentication. Now instead of manually deploying the keycloak server while running Junit test cases, I want to automate the keycloak deployment step. I want to ask that is there any Java library through which I can easily start the keycloak, pass the json file that contains all the configuration related to the realm, clients and users and then after running all Junit test cases keycloak automatically stops? Is there any way of doing such thing while running Junit test cases easily?
If you start an external service, this are surely not unit-tests. This is not even integration testing. That's part of end-to-end testing.
With Spring Boot:
the first (unit) are #WebMvcTest (or #WebFluxTest for reactive app) when testing a #Controller or plain JUnit with #ExtendWith(SpringExtension.class) to #EnableMethodSecurity and auto-wire an instrumented instance of the secured #Component (#Service or #Repository with #PreAuthorise, #PostFilter and alike). All external service and all #Components (but the one under test) are mocked and so is the Authentication in the security-context.
the second (integration) are #SpringBootTest which wire together the application #Components (but external services are still mocked / stubbed). It is still possible to #AutoConfigureMockMvc (or #AutoConfigureWebTestClient) and as so, to keep using mocked Authentication.
the last is best to be done without Java (Protractor or whatever end-to-end testing tool)
I answered this subject many times already. To mention a few:
https://stackoverflow.com/a/75465772/619830
How to write unit test for SecurityConfig for spring security
https://stackoverflow.com/a/75376543/619830

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 Test doesn't rollback REQUIRES_NEW, any workaround?

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?

Testing with spring configuration and Mocking objects

I am using a spring configuration in testing like below:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:config/spring/config.xml" })
Let's say I'm testing a service that depends on other services and a repository. Using the #ContextConfiguration these beans will be created and autowired into the service I am testing.
Is it considered better to mock these dependencies and repositories? When creating and loading a test configuration as done currently, a problem in a dependency could cause the test to fail, so I am not really only testing that one service but the other dependencies as well.
Should these dependencies be mocked in the unit test (since you are only supposed to test the functionality of that class and not its dependencies) but instantiated and wired in during integration tests?

Junit for application using Struts, Springs, Hibernate all

Is there any specific approach already defined or used by any one for unit testing applications using Struts, Springs, Hibernate technologies ?
What are different mocking apis we can use ?
Note: I don't want want any solution to individually unit test these technologies.
You can inject mock Spring facades into your Struts actions, and unit-test the Struts actions. You can inject mock Spring repositories/DAOs into your Spring facades and unit-test these facades. And you can use DBUnit (or another similar tool) to populate a test database before each repository test.
Choose the mocking API you like the most. There are sevaral ones: Mockito, EasyMock, JMock, etc.
If you want to test the whole app, you're not doing unit tests anymore, but integration tests. And mocking would go against your goal. If that's what you want to do, you could use Selenium or HtmlUnit to test the application.

Resources