Class EmbeddedDatabaseRule
java.lang.Object
org.junit.rules.ExternalResource
com.github.mjeanroy.dbunit.integration.spring.junit4.EmbeddedDatabaseRule
- All Implemented Interfaces:
org.junit.rules.TestRule
public class EmbeddedDatabaseRule
extends org.junit.rules.ExternalResource
A JUnit 4
Note that:
Rule
that can be used to start/stop embedded database.
Note that:
-
When used as a
ClassRule
, the embedded database will be started before all tests, and stopped after all tests. -
When used as a
Rule
, the embedded database will be started before each test, and stopped after each test.
ClassRule
annotation:
public class MyDaoTest {
@ClassRule
public static EmbeddedDatabaseRule rule = new EmbeddedDatabaseRule(
new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.addScript("classpath:/sql/init.sql")
.addScript("classpath:/sql/data.sql")
.build()
);
@Test
public void test1() throws Exception {
Assert.assertEquals(2, count(rule.getDb().getConnection()));
}
}
-
Constructor Summary
ConstructorsConstructorDescriptionCreate rule with default builder.EmbeddedDatabaseRule
(Class<?> testClass) Create rule and lookup forEmbeddedDatabaseConfiguration
in thetestClass
to initialize the embedded database.EmbeddedDatabaseRule
(org.springframework.jdbc.datasource.embedded.EmbeddedDatabase db) Create rule. -
Method Summary
Methods inherited from class org.junit.rules.ExternalResource
apply
-
Constructor Details
-
EmbeddedDatabaseRule
public EmbeddedDatabaseRule(org.springframework.jdbc.datasource.embedded.EmbeddedDatabase db) Create rule.- Parameters:
db
- Embedded database.
-
EmbeddedDatabaseRule
public EmbeddedDatabaseRule()Create rule with default builder. -
EmbeddedDatabaseRule
Create rule and lookup forEmbeddedDatabaseConfiguration
in thetestClass
to initialize the embedded database.- Parameters:
testClass
- The tested class.
-
-
Method Details
-
before
protected void before()- Overrides:
before
in classorg.junit.rules.ExternalResource
-
after
protected void after()- Overrides:
after
in classorg.junit.rules.ExternalResource
-
getDb
public org.springframework.jdbc.datasource.embedded.EmbeddedDatabase getDb()Gets currently created database instance.- Returns:
- Database instance, may be
null
until rule has been initialized.
-