How to inspect database when running with spring-db-unit - spring

We run our tests with spring-db-unit, the forked and maintained one.
The tests run just fine and can access and modify the data. But when I was debugging some tests I realized that the database stays empty all the time! My assumption is that the runner creates a transaction and rolls it back at the end of the test instead of commiting.
How can I inspect the data or persist it in the database at the end (or anytime, really!) of a test?
The setup:
Our config is follows the suggested setup from the gibthub readme:
#TestExecutionListeners(
inheritListeners = false,
value = {
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class,
WithSecurityContextTestExecutionListener.class
})
#DatabaseSetup(value = "/testdata/dbunit/base_test_dataset.xml", type = DatabaseOperation.CLEAN_INSERT)
#SpringApplicationConfiguration(classes = {
DBUnitTestConfig.class,
})
#DbUnitConfiguration(databaseConnection = "dbUnitConnection")
public class ReviewServiceTest {
#Test
#WithUserDetails(TEST_COCKPIT_1)
public void getUserVersionReviews() throws Exception {
// when in a debug point here, the DB is empty.
}
}
The database is PostgreSQL 11.4. The DBUnitTestConfig class above just configures the PostgresqlDataTypeFactory of the DatabaseConfigBean
Switching to TransactionDbUnitTestExecutionListener instead of TransactionalTestExecutionListener and DbUnitTestExecutionListener didn't help.
I tried it with DatabaseOperation.INSERT instead of the default one and moved the #DatabaseSetupannotation onto test methods and setUp methods, without success.
We are using the following dependencies. It's not latest and the furthest I was able upgrade for now :
com.github.ppodgorsek:spring-test-dbunit-core:5.2.0
org.dbunit:dbunit:2.6.0
junit:junit:4.12
org.springframework.boot:spring-boot-starter-parent:1.3.8.RELEASE
OpenJDK 1.8
What confuses me that I couldn't find anyone complaining or configuration options for it, so I must be missing something obivious!

Rolling back transactions isn't spring-test-dbunit fault. It works with and without transactions. The roll back is the default behavior of the TransactionalTestExecutionListener.
Citing the javadoc of the class:
By default, test transactions
will be automatically rolled back after completion of the
test; however, transactional commit and rollback behavior can be
configured declaratively via the #Rollback annotation
at the class level and at the method level.
The solution is to put #org.springframework.test.annotation.Rollback(false) on the test class or method.

Related

Repository inserts don't work in SpringBootTest with WebTestClient

I am converting a spring boot application to use webflux by making the endpoints reactive one at a time. Most of the (Spock) tests worked fine, but the ones where I autowire the JPA repository in the test to insert data do not. The repository is always empty when the resource (that we're testing) reads from it. I have tens of tests like this with different repositories and they all have same issue.
Here is an example of a test (the resource we're testing just does findById and returns the example from the repository):
#SpringBootTest
#AutoConfigureWebTestClient(timeout = "60000")
#Transactional
class PaymentControllerIntegrationTest extends Specification {
#Autowired
WebTestClient client
#Autowired
PaymentRepository repo
def "GET /example/{id} returns correct example"() {
given:
def needle = new Example(id: 1L)
def haystack = repo.saveAll([needle, new Example(id: 2L), new Example(id: 3L)])
when:
def response = client.get().uri(EXAMPLE_URL, [id: needle.id.toString()]).exchange()
then:
response.expectStatus().isOk()
response.returnResult(ExampleResponse.class).getResponseBody().blockLast().id == needle.id
}
When I put a breakpoint in the controller and do findAll() the repository is always empty.
I have tried using TestEntityManager.persistAndFlush instead
I have tried using repo.saveAllAndFlush instead
I have tried autowiring ApplicationContext and then building WebTestClient from that, but I never got the autowiring to work
My best guess currently is that the test is setup incorrectly (application context?) so the repository in the test is not the same as the repository in the application.
When using the TestWebClient (or the TestRestTemplate) you are actually issuing a real HTTP request to your started server. This request is handled in a different thread and as such uses a new transaction.
Your test is transactional as well but the data hasn't been committed, another transaction can only read committed data (or you need to set the isolation level to READ_UNCOMMITTED but that is probably not something you should or want to do).
When using MockMvc you are replacing the actual container with a mocked instance and it uses a MockHttpServletRequest etc. and executes in the same thread (and thus reuse the same transaction and can see the data).
To solve, make your test not transactional and cleanup the data afterwards. This will however impact the performance of your tests (as committing and then deleting is slower as rolling back a transaction after the test).

Integration Flow Test Spring Transaction

I'm currently writing a Spock integration test for my Spring application.
I'd like to use #Stepwise in order to perform a test which interacts with the database and then have the next test build on top of the data left behind from the first test.
Unfortunately it seems that a new transaction is started for every test method, thus clearing the data I need to build upon. Rollback(false) does not prevent this behaviour, since the whole transaction is discarded AFAIK.
Here's an example, the MyUserService interacts with a #Repository-interface.
#Stepwise
#SpringBootTest
#TestPropertySource(locations = "classpath:application-test.properties")
class MyServiceImplIntegrationFlowSpec extends Specification {
#Autowired
#Subject
MyUserService myUserService
#Shared
String userId
void "create user"() {
when:
userId = myUserService.createUser()
then:
userId
}
void "change user permission"() {
when:
myUserService.changePermission(userId, "read")
then:
myUserService.fetchPermission() == "read"
}
}
How can I reuse the data which was created by the previous test method, as is commonly done with #Stepwise, in conjunction with database operations?
The Spring Test framework rolls back the data of each test method by default. You can change this default behaviour by adding the #Commit annotation to each of your test methods where you want to keep the changes in the database. If the whole test suite should commit data to the database I think you can put the #Commit annotation also on class level.
See the reference https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#testing-tx
It says:
One common issue in tests that access a real database is their effect
on the state of the persistence store. Even when you use 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 of a transaction.
and continues describing with
The TestContext framework addresses this issue. By default, the
framework creates and rolls back a transaction for each test. You can
write code that can assume the existence of a transaction. If you call
transactionally proxied objects in your tests, they behave correctly,
according to their configured transactional semantics. In addition, if
a test method deletes the contents of selected tables while running
within the transaction managed for the test, the transaction rolls
back by default, and the database returns to its state prior to
execution of the test. Transactional support is provided to a test by
using a PlatformTransactionManager bean defined in the test’s
application context.
If you want a transaction to commit (unusual, but occasionally useful
when you want a particular test to populate or modify the database),
you can tell the TestContext framework to cause the transaction to
commit instead of roll back by using the #Commit annotation.
Your test case could look like
#Stepwise
#SpringBootTest
#TestPropertySource(locations = "classpath:application-test.properties")
#Commit // if you want all test methods to commit to the database
class MyServiceImplIntegrationFlowSpec extends Specification {
#Autowired
#Subject
MyUserService myUserService
#Shared
String userId
#Commit // if a single test needs to commit to the database
void "create user"() {
when:
userId = myUserService.createUser()
then:
userId
}
void "change user permission"() {
when:
myUserService.changePermission(userId, "read")
then:
myUserService.fetchPermission() == "read"
}
}

Spring Database Integration Test, when to flush or?

I am fairly new to spring, and doing some integration tests.
Using Hibernate, MySql and Spring data JPA.
I am using transaction support and everything gets rolled back at the end of each test.
For example:
#Test (expected=DataIntegrityViolationException.class)
public void findAndDelete() {
UUID uuid = UUID.fromString(TESTID);
User user= iUserService.findOne(uuid);
iUserService.delete(cashBox);
iUserService.flush();
assertNull(iUserService.findOne(uuid));
}
In the above code, I call the iUserService.flush(), so that the sql gets sent to the DB, and an expected DataIntegrityViolationException occurs because there is a foreign key from User to another table (Cascade is not allowed, None). All good so far.
Now, if I remove the iUserService.flush()
then the expected exception does not occur because the sql does not get sent to the DB.
I tried adding the flush() into a teardown #After method, but that didn't work as the test does not see the exception outside of the test method.
Is there any way to avoid calling the flush within the test methods?
It would be preferable if the developers on my team did not have to use the flush method at all in their testing code
Edit:
I tried adding the following
#Before
public void before() {
Session session = entityManagerFactory.createEntityManager().unwrap(Session.class);
session.setFlushMode(FlushMode.ALWAYS);
}
but it does seem to flush the sqls, before each query.
In my humble opinion, it's better than the developers of your team know what they are doing.
It includes the things that are configured by default and the consequences of that.
Please, take a look to why you need avoid false positives when testing ORM code

For test suite, replacing MYSQL by in memory HSQLDB is not working

We have an application where we use Struts, Spring, and hibernate.
Previously, we were using mysql databse for running test suites using testng framework.
Now we want to use “in memory” database of HSQLDB.
We have made all the required code changes to use HSQLDB in “in memory” mode.
For ex.
Datasource url = jdbc:hsql:mem:TEST_DB
Username = sa
Password =
Driver = org.hsqldb.jdbcDriver
Hibernate dialect= org.hibernate.dialect.HSQLDialect
Hibernate.hbm2ddl.aoto = create
#Autowired
private DriverManagerDataSource dataSource;
private static Connection dbConnection;
private static IDatabaseConnection dbUnitConnection;
private static IDataSet dataSet;
private MockeryHelper mockeryHelper;
public void startUp() throws Exception {
mockeryHelper = new MockeryHelper();
if (dbConnection == null) {
dbConnection = dataSource.getConnection();
dbUnitConnection = new DatabaseConnection(dbConnection);
dbUnitConnection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
dataSet = new XmlDataSet(new FileInputStream("src/test/resources/test-data.xml"));
}
DatabaseOperation.CLEAN_INSERT.execute(dbUnitConnection, dataSet);
}
We have done required code changes to our base class where we do startup and teardown of database before and after each test.
We use test-data.xml file from where we insert test data to created database using testng framework. Now my questions are
1.when I run test case, database gets created and data is also inserted correctly. However, my respective daos return empty object list when I try to retrieve them from interceptors of struts.
2.We use HSQLDB version 1.8.0.10. Same configurations are made for other project. In that project, most of the test cases are running with success, however for some of them sorting order of data is incorrect.
We discovered that HSQLDB is case sensitive for sorting. And there is one property sql.ignore_case, when set to true, sorting becomes case insensitive. But this is not working for us.
Can someone please help in this?
Thanks in adavance.
I'm afraid sql.ignore_case is not available in your HSQLDB version, as it's not even in the last stable (2.2.9), contrary to what the docs say. However latest snapshots, as stated in this thread, do include it. I'm not using 1.8 my self, but executing SET IGNORECASE TRUE before any table creation may work for you, it does in 2.2.9. If you really need 1.8, a third option may be to pick the relevant code from latest source, add it to 1.8 source and recompile, no idea how hard/easy this could be.

Functional test in Spring: changes in database not visible in test

I have integration test (which runs under Jetty) where I open page (by using Selenium) and check that record about this activity was added to database (HSQL). But it does not work -- JPA (Hiberante) adds record (I see it in logs) but when I execute SELECT query there no records at all.
Test case:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {
"classpath:spring/DispatcherServletContext.xml"
})
#TransactionConfiguration(defaultRollback = false)
public class WhenUserOpenNotExistingPage
extends WhenUserAtAnyPage<NotFoundErrorPage> {
private final String currentUrl;
#Autowired
private SuspiciousActivityDao suspiciousActivities;
private String generateRandomUrl() {
return String.format(
"/tests/page-does-not-exists-%s.htm",
RandomStringUtils.randomNumeric(5)
);
}
public WhenUserOpenNotExistingPage() {
currentUrl = generateRandomUrl();
page.open(currentUrl);
}
#Test
#Transactional(readOnly = true)
public void incidentShouldBeLoggedToDatabase() {
SuspiciousActivity activity =
suspiciousActivities.findByPage(currentUrl);
assertNotNull(activity);
}
}
Also WhenUserOpenNotExistingPage() (constructorr) called twice (and I don't know why it happens and probably is the root of my problem).
Can you help me?
Thanks in advance!
I assume you are adding something to the database in the test case and the same database is being used by your application running on Jetty. If your database uses any isolation level above read uncommited, the changes you made in the test case won't be visible until that test case finishes. This is because you database code joins the transaction that was created when the test was starting.
By default this test transaction is rolled back after the test finishes, so the changes are visible within the current test (transaction), but aren't visible outside (by different threads/connections) and are rolled back. You are changing the default behaviour by using defaultRollback = false attribute, but this only means that changes you made in one test aren't visible by the web application (different database connection), but will be visible in subsequent test (after commit). Not really useful.
You have few options:
Get rid of Spring transactional test support. This means Spring won't create a new transaction every time you start a test and won't do commit/rollback. Now it is up to you when to start transaction and commit it before actually starting SElenium test.
This can easily be done by replacing #TransactionConfiguration with:
#TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class})
this will override default test execution listeners, removing TransactionalTestExecutionListener from the stack
You might run setup code from different thread or with propagation REQUIRES_NEW
Finally, you should consider doing setup outside of JUnit. Maybe you can do the setup from the application itself?
As for the constructor - new instance of JUnit test class is created per test. The authors of JUnit claim it makes the test more predictable and stateless (no dependencies between test) by cleaning up the test class prior to running every test. In practice, especially with integration tests, this is more a pain than a advantage.
By the way if you do some database manipulation in the test and commit the changes, watch out for test dependencies. The order in which tests are executed is not guaranteed in JUnit. This means that changes in one test shouldn't affect other tests.

Resources