I am trying to use H2 or HSQL for my unit testing. But my application is not of spring and hibernate. It seems most of the references are there only with spring and hibernate for HSQL/H2 in memory db for unit testing.
Can someone point to a right reference where only hsql/h2 is used with junit plainly? Appreciate your time.
I ususally do something like this:
In the #Before method I establish a connection to a memory database, something like this:
#Before
public void setup()
{
this.dbConnection = DriverManager.getConnection("jdbc:hsqldb:mem:testcase;shutdown=true", "sa", null);
}
The connection is stored in an instance variable, so it's available for each test.
Then if all tests share the same tables, I also create those inside the setup() method, otherwise each tests creates its own tables:
#Test
public void foo()
{
Statement stmt = this.dbConnection.createStatement();
stmt.execute("create table foo (id integer)");
this.dbConnection.commit();
... now run the test
}
In the #After method I simplic close the connection which means the in-memory database gets wiped and the next test runs with a clean version:
#After
public void tearDown()
throws Exception
{
dbConnection.disconnect();
}
Sometimes I do need to run unitt-tests agains a real database server (you can't test Postgres or Oracle specific features using HSQLDB or H2). In that case I establish the connection only once for each Testclass instead of once for each test method. I then have methods to drop all objects in order to cleanup the schema.
This can all be put into a little utility class to avoid some of the boilerplate code. Apache's DbUtils can also make life easier as will DbUnit if you want to externalize the test data somehow.
I know I am a little late to the party :-)
I had the same issue a while back and created a JUNIT integration that use the #Rule mechanism to setup an in-memory database for JUnit tests. I found it to be a real easy and good way to be able to test my database integration code. Feedback is more than welcome.
The source code and instructions for use can be found at https://github.com/zapodot/embedded-db-junit
Related
I have a side project were I'm using Spring Boot, Liquibase and Postgres.
I have the following sequence of tests:
test1();
test2();
test3();
test4();
In those four tests I'm creating the same entity. As I'm not removing the records from the table after each test case, I'm getting the following exception: org.springframework.dao.DataIntegrityViolationException
I want to solve this problem with the following constraints:
I don't want to use the #repository to clean the database.
I don't want to kill the database and create it on each test case because I'm using TestContainers and doing that would increase the time it takes to complete the tests.
In short: How can I remove the records from one or more tables after each test case without 1) using the #repository of each entity and 2) killing and starting the database container on each test case?
The simplest way I found to do this was the following:
Inject a JdbcTemplate instance
#Autowired
private JdbcTemplate jdbcTemplate;
Use the class JdbcTestUtils to delete the records from the tables you need to.
JdbcTestUtils.deleteFromTables(jdbcTemplate, "table1", "table2", "table3");
Call this line in the method annotated with #After or #AfterEach in your test class:
#AfterEach
void tearDown() throws DatabaseException {
JdbcTestUtils.deleteFromTables(jdbcTemplate, "table1", "table2", "table3");
}
I found this approach in this blog post:
Easy Integration Testing With Testcontainers
Annotate your test class with #DataJpaTest. From the documentation:
By default, tests annotated with #DataJpaTest are transactional and roll back at the end of each test. They also use an embedded in-memory database (replacing any explicit or usually auto-configured DataSource).
For example using Junit4:
#RunWith(SpringRunner.class)
#DataJpaTest
public class MyTest {
//...
}
Using Junit5:
#DataJpaTest
public class MyTest {
//...
}
You could use #Transactional on your test methods. That way, each test method will run inside its own transaction bracket and will be rolled back before the next test method will run.
Of course, this only works if you are not doing anything weird with manual transaction management, and it is reliant on some Spring Boot autoconfiguration magic, so it may not be possible in every use case, but it is generally a highly performant and very simple approach to isolating test cases.
i think this is the most effecient way for postgreSQL. You can make same thing for other db. Just find how to restart tables sequence and execute it
#Autowired
private JdbcTemplate jdbcTemplate;
#AfterEach
public void execute() {
jdbcTemplate.execute("TRUNCATE TABLE users" );
jdbcTemplate.execute("ALTER SEQUENCE users_id_seq RESTART");
}
My personal preference would be:
private static final String CLEAN_TABLES_SQL[] = {
"delete from table1",
"delete from table2",
"delete from table3"
};
#After
public void tearDown() {
for (String query : CLEAN_TABLES_SQL)
{
getJdbcTemplate().execute(query);
}
}
To be able to adopt this approach, you would need to
extend the class with DaoSupport, and set the DataSource in the constructor.
public class Test extends NamedParameterJdbcDaoSupport
public Test(DataSource dataSource)
{
setDataSource(dataSource);
}
I know how to configure Spring/JUnit to rollback after each test case.
What I am after is a way to start and roll back one transaction for all test cases.
I am using #BeforeClass to prepare my HSQL DB for several test cases. Then I want to rollback the changes after the end of all test cases in #AfterClass.
What is the best way to achieve this rollback?
Here is my code example:
#BeforeClass
public static void setupDB(){
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:/spring/applicationContext-services-test.xml");
//- get beans and insert some records to the DB
...
}
#AfterClass
public static void cleanUp(){
??? what should go here?
}
Any idea on the best way to do rollback in AfterClass?
Thanks to all..
In case it's acceptable to you to roll back after each test and to use Spring, the following snippet from my project might help you:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:/net/sukharevd/shopzilla/model/application-context-dao.xml" })
#TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
#TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
#Transactional
public class HibernateCategoryDaoTest extends AbstractTransactionalJUnit4SpringContextTests {
You probably don't want to be doing a rollback after the class, but after each test. Tests are not guaranteed to run in the same order each time, so you might get different results. Unit tests should be isolated.
You can use the spring test runner to annotate your class and rollback transactions
#RunWith(SpringJUnit4ClassRunner.class)
#TransactionConfiguration(defaultRollback=true)
public static class YourTestClass {
#Test
#Transactional
public void aTest() {
// do some db stuff that will get rolled back at the end of the test
}
}
In general though you should also try to avoid hitting a real database in unit tests. Tests that hit the database are generally integration level tests (or even more coarse grain tests like acceptance tests). The DBUnit http://www.dbunit.org/ framework is used to stub out databases for unit tests so you dont' need to use a real database.
I managed to solve this issue using just Spring/JUnit (without using DBUnit).
In short, the solution was to call transactionManager.getTransaction(def).setRollbackOnly(); in #BeforeClass.
Let me first explain what I was trying to do.
My main motive was around this flow:
1. Start transaction
2. Insert load test data
3. run several test cases on the same test data
4. rollback test data.
Since I am building my load test data in #BeforeClass, I was looking to rollback in #AfterClass. This seems to be unnecessary as I can simply instruct the transaction to be rollback only in my #BeforeClass!
So here is how I did it:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = "classpath:/spring/applicationContext-services-test.xml")
#TestExecutionListeners(inheritListeners = false, listeners = { SpecialDependencyInjectionTestExcecutionListener.class })
#TransactionConfiguration(defaultRollback = true)
#Transactional
public class loadTest {
...
private static HibernateTransactionManager transactionManager;
...
#BeforeClass
public static void setupDB() {
//- set the transaction to rollback only. We have to get a new transaction for that.
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
transactionManager.getTransaction(def).setRollbackOnly();
...
//- start loading the data using the injected services.
...
}
This helped to rollback at the end of the class.
P.S. The SpecialDependencyInjectionTestExcecutionListener is an extension to DependencyInjectionTestExecutionListener which I used to override beforeTestClass to force the application.context to to be loaded before calling #BeforeClass. Credit goes to Dmitriy in highlighting this Listener which was the hint to solve another problem which i had in my mind.
Thanks to everyone who helped in highlighting and suggestions which collectively led me to this solution.
Dhafir
Let's pretend you weren't using Spring. (Because using Spring the other answers here are better.) Then you would have three choices:
Use dbunit to take care of the data load/cleanup for you. (The site is down at the moment but if you Google it, you can see some tutorials.)
Create manual deletes for the manual updates
Create a rollback point in your database as the first step of setup. Here's how to do so in Oracle.
I have searched around but have been unsuccessful in determining whether the following approach is possible / good practice. Basically what I would like to do is the following:
Create JUnit Tests which use DBUnit to initialize the data. Run multiple test methods each which run off of the same initial dataset. Rollback after each test method back to the state right after the initial setUp function. After all test methods have run rollback any changes that were made in the setUp function. At this point the data in the database should be exactly the same as it was prior to running the JUnit test class.
Ideally I would not have to reinitialize the data before each test case because I could just roll back to the state right after setUp.
I have been able to roll back individual test methods but have been unable to roll back changes made in setUp after all test methods have run.
NOTE: I am aware of the different functionality of DBUnit such as CLEAN_INSERT, DELETE etc. I am using the Spring framework to inject my dataSource.
An example layout would look like:
public class TestClass {
public void setUp() {
// Calls a method in a different class which uses DBUnit to initialize the database
}
public void runTest1() {
// Runs a test which may insert / delete data in the database
// After running the test the database is in the same state as it was
// after running setUp
}
public void runTest2() {
// Runs a test which may insert / delete data in the database
// After running the test the database is in the same state as it was
// after running setUp
}
// After runTest1 and runTest2 have finished the database will be rolled back to the
// state before any of the methods above had run.
// The data will be unchanged as if this class had never even been run
}
I would be running the tests in a development database however I would prefer to not affect any data currently in the database. I am fine with running CLEAN_INSERT at the start to initialize the data however after all test methods have run I would like the data back to how it was before I ran my JUnit test.
Thanks in advance
Just as "setUp" JUnit offers a "tearDown" method executed after each test method. that you could use to rollback. Also starting with JUnit 4 you have the following annotations:
#BeforeClass: run once before running any of your tests in the test case
#Before: run every time before a test method
#After: run every time after a test method
#AfterClass: run once after all your tests in the current suite have been executed
We solved a similar problem at the oVirt open source project. Please take a look at the code residing at engine\backend\manager\modules\dal\src\test\java\org\ovirt\engine\core\dao\BaseDAOTestCase.java.
In general look at what we did there at the #BeforeClass and #AfterClass methods. You can use it on per method basis. We used the Spring-test framework for that.
In my project, I've used spring, jpa with PostgreSQL DB,
I've lots of table in DB and I need to have Unit testing of all of them.
Is there any framework which just rollback all the transactions after each test finished so every test will have fresh/same DB data to Test. And this way after all Test executions, data of DB schema would be as it is.
Any suggestion for this?
I've some idea of DBUnit but in that I need to write .xml files for every input data for every test and need to insert data in setup() and clear/remove data in tearDown(), but doesn't seems better strategy to me.
Any suggestion is appreciated.
Thanks.
As #Ryan indicates .... the Testing section of the Spring Reference manual should be consulted.
Some startup tips...
We've handled this using Spring's AbstractTransactionalJUnit4SpringContextTests.
For example, we define an abstract superclass:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("file:WebContent/WEB-INF/testconfig/test-web-application-config.xml")
#TransactionConfiguration()
#Transactional
public abstract class OurAbstractTransactionalSpringContextTest extends AbstractTransactionalJUnit4SpringContextTests {
}
And then individual subclasses which need additional context get defined as:
#ContextConfiguration("classpath:path/to/config/ConfigForTestCase.xml")
public class TestOurFunction extends OurAbstractTransactionalSpringContextTest {
#Test
public void testOurMethod() {
}
}
Note that:
Not all test classes need additional context for them, skip the #ContextConfiguration on the particular subclass.
We execute via ant and use the forkmode="perBatch" attribute on the junit task. That ensures all tests run with the same context configuration (saves from reloading the Spring context for each test). You can use the #DirtiesContext to indicate that the context should be refreshed after a method/class.
mark each method with the #Test annotation. The Spring framework doesn't pick up methods using Junit's public void testXXX() convention.
Is there any framework which just rollback all the transactions after each test finished so every test will have fresh/same DB data to Test. And this way after all Test executions, data of DB schema would be as it is.
From my other answer posted earlier in the day, yes, this is possible using DbUnit. (Based on your edit, you don't need this; the subsequent section of my answer addresses why I use DbUnit, and when I wouldn't use it).
The following code snippet demonstrates how the setup of every test is performed:
#Before
public void setUp() throws Exception
{
logger.info("Performing the setup of test {}", testName.getMethodName());
IDatabaseConnection connection = null;
try
{
connection = getConnection();
IDataSet dataSet = getDataSet();
//The following line cleans up all DbUnit recognized tables and inserts and test data before every test.
DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
}
finally
{
// Closes the connection as the persistence layer gets it's connection from elsewhere
connection.close();
}
}
private IDatabaseConnection getConnection() throws Exception
{
#SuppressWarnings({ "rawtypes", "unused" })
Class driverClass = Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection jdbcConnection = DriverManager.getConnection(jdbcURL, "XXX",
"YYY");
IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
return databaseConnection;
}
private IDataSet getDataSet() throws Exception
{
ClassLoader classLoader = this.getClass().getClassLoader();
return new FlatXmlDataSetBuilder().build(classLoader.getResourceAsStream("database-test-setup.xml"));
}
The database-test-setup.xml file contains the data that will be inserted into the database for every test. The use of DatabaseOperation.CLEAN_INSERT in the setup method ensures that all the tables specified in the file will be cleared (by a delete of all rows) followed by an insert of the specified data in the test data file.
Avoiding DbUnit
I use the above approach specifically to clear out sequences before the start of every test, as the application uses a JPA provider which updates the sequences in a separate transaction. If your application is not doing anything like that, then you can afford to simply start a transaction in your setup() method and issue a rollback on teardown after the test. If my application didn't use sequences (and if I didn't desire to reset them), then my setup routine would have been as simple as:
#Before
public void setUp() throws Exception
{
logger.info("Performing the setup of test {}", testName.getMethodName());
// emf is created in the #BeforeClass annotated method
em = emf.createEntityManager();
// Starts the transaction before every test
em.getTransaction.begin();
}
#After
public void tearDown() throws Exception
{
logger.info("Performing the teardown of test {}", testName.getMethodName());
if (em != null)
{
// Rolls back the transaction after every test
em.getTransaction().rollback();
em.close();
}
}
Also, I use dbdeploy with Maven, but that is primarily for keeping the test database up to date with the versioned data model.
Spring's test framework does exactly that for you.
I'd handled it following ways.
When the project is in test mode. I'd used bootstraping data to to test using dbdeploy
Fixed data that you can assert on. and use the dao directly to test the DAO and DB layer of your application.
Hope it helps
Update
for example there is an entity called Person in your system, now what you can test on this is basic CRUD operations.
Run bootstraping data scripts to laod the data
retrieve all the persons from DB and assert on it. like wise see all the CRUD
to rollback the transaction you can mark
#TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
so it will rollback the DB stuff
This question already has answers here:
Spring + JUnit + H2 + JPA : Is it possible to drop-create the database for every test?
(3 answers)
Closed 5 years ago.
My set up is like this.
H2 in Memory database connected to using JPA the persistence context is configured using Spring. The database set up script is run by Flyway to generate my tables and add in data.
Database URL: jdbc:h2:mem:test_data
In our JUnit tests we have setUp and tearDown Methods like this
#Override
protected void setUp() throws Exception {
this.context = new ClassPathXmlApplicationContext("classpath:/DataContext.xml");
this.data = this.context.getBean(DataProvider.class);
}
#Override
protected void tearDown( ) throws Exception {
this.context.close();
this.context = null;
this.data = null;
}
The intention here is for each JUnit test to get it's own data base, and this does seem to work sometimes, however there seem to be occasions when it appears like I am getting the same database as the previous test. An example is that I have 2 tests:
public void testGetFoos()
{
Collection<Foo> foos= data.getFoos();
assertEquals(NUMBER_OF_FOOS,foos.size());
}
public void testSaveFoos()
{
Foo bar = makeDummyFoo();
data.saveFoo(bar);
Collection<Foo > foos = data.getFoos();
assertEquals(NUMBER_OF_FOOS + 1,foos.size());
}
These methods should be able to run in any order and when running the maven build that appears to be the case, they are able to run independently without affecting each other, however occasionally when running the JUnit test class individually from eclipse the save method will run first, then the get method will run and get the wrong count because it seems to have gotten the same in mem database as the previous test method.
So my question is how to I kill my H2 in memory database between JUnit tests using my set up with Spring? or What could be causing it to not shut down properly, if the context.close() method is the correct way?
I believe using DbUnit would be a better approach; it is intended for exactly this purpose. Give it a look.
Good luck.
I suggest that you use :
#Test annotations on test methods
#Before on setup
#After on teardown
You can put this code in #Before to ensure that all #Test methods will have fresh db
Connection connection = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
Statement stmt = connection .createStatement()
stmt.execute("DROP ALL OBJECTS");
connection.commit();
connection.close();
You could use Spring's #Sql annotation to setup and teardown the database after each test method on your junit test like this:
#SqlGroup({
#Sql(executionPhase = ExecutionPhase.BEFORE_TEST_METHOD, scripts ="classpath:db/data.sql"),
#Sql(executionPhase = ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:db/clean.sql")
})
public void test(){
//test logic
}