![]() I use JUnit 5 in my test suite, and make use of the extension system. If you were using MySQL instead of H2, you might use mysqldump toĬreate a snapshot of the "backup" database after step 1,Īnd restore it to the "primary" database in step 2. After each test runs, I drop the "primary" database.Before each test runs, I copy the "backup" database to a.Before any tests run, run all of my schema migrations againstĪ "backup" H2 memory database. ![]() In my test suite, I achieve this by doing the following: Test-specific fixtures for certain test groups, and ensure that whateverĬhanges a given test makes to the database do not carry over to the Want to fill H2 with your application's database schema once, add Your use case may vary, but there's a chance that, like me, you'll createInjector ( jpaModule /*, other modules */ ) Seeding and cleaning properties ( jpaProperties ) Injector injector = Guice. put ( "", "jdbc:h2:mem:test?MODE=MYSQL" ) JpaPersistModule jpaModule = new JpaPersistModule ( ' the - name - of - a - unit - defined - in - persistence. put ( "", "org.h2.Driver" ) jpaProperties. put ( "hibernate.dialect", "2Dialect" ) jpaProperties. Properties jpaProperties = new Properties () jpaProperties. The H2 is in the classpath, and configuring a few properties: Then connecting to H2 in a test is, at a minimum, a matter of ensuring Connecting to H2 in a testĪssuming that your application uses Hibernate to connect to to MySQL, Talk more about using it as a substitute for MySQL in functional tests. However, let's pretend both I have good reasons for using H2, and I should have given more careful consideration to using something like Outside the JVM, and should be fast (in theory). Meaning it doesn't require interaction with processes or filesystems Simplest option by virtue of being written in Java and fully in-memory, It appeared at the time I chose it to be the I can't say that I have a particularly good justification for choosing The one I've chosen, is to use a pure Java, in-memory database with Test suite start up an instance, perhaps using something like Start up a MySQL instance before the tests run, or have the There's more than one way to connect a test suite to a database: Much more valuable to have tests that exercise an actual database. Most of the code in this application I amĬontent to test with unit tests, but, for the DAL layer, I find it A Java application I develop has a data access layer that interacts
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |