From 5de9137d909e5d22a168ca7d17d9429df59ad6b1 Mon Sep 17 00:00:00 2001 From: rmpestano Date: Thu, 16 Jun 2016 16:16:02 -0300 Subject: [PATCH] refs #1 - expected dataset bdd documentation --- .../dbunit/rules/ExpectedDataSetCDIIt.java | 5 + .../dbunit/rules/bdd/DBUnitRulesBdd.java | 3 +- .../bdd/cdi/CDISeedDatabaseStepdefs.java | 16 --- .../features/cdi/cdi-seed-database.feature | 2 + .../general/dataset-replacements.feature | 4 +- .../features/general/expected-dataset.feature | 102 ++++++++++++++++++ .../dbunit/rules/ExpectedDataSetIt.java | 19 ++-- 7 files changed, 125 insertions(+), 26 deletions(-) create mode 100644 cdi/src/test/resources/features/general/expected-dataset.feature diff --git a/cdi/src/test/java/com/github/dbunit/rules/ExpectedDataSetCDIIt.java b/cdi/src/test/java/com/github/dbunit/rules/ExpectedDataSetCDIIt.java index 6b3e977..d109f95 100644 --- a/cdi/src/test/java/com/github/dbunit/rules/ExpectedDataSetCDIIt.java +++ b/cdi/src/test/java/com/github/dbunit/rules/ExpectedDataSetCDIIt.java @@ -13,12 +13,16 @@ /** * Created by rafael-pestano on 16/06/2016. */ +// tag::expectedCDIDeclaration[] @RunWith(CdiTestRunner.class) public class ExpectedDataSetCDIIt { @Inject EntityManager em; +// end::expectedCDIDeclaration[] + + // tag::expectedCDI[] @Test @UsingDataSet //needed to activate interceptor (can be at class level) @ExpectedDataSet(value = "yml/expectedUsers.yml",ignoreCols = "id") @@ -32,4 +36,5 @@ public void shouldMatchExpectedDataSet() { em.persist(u2); em.getTransaction().commit(); } + // end::expectedCDI[] } diff --git a/cdi/src/test/java/com/github/dbunit/rules/bdd/DBUnitRulesBdd.java b/cdi/src/test/java/com/github/dbunit/rules/bdd/DBUnitRulesBdd.java index a40e1ef..66d8e27 100644 --- a/cdi/src/test/java/com/github/dbunit/rules/bdd/DBUnitRulesBdd.java +++ b/cdi/src/test/java/com/github/dbunit/rules/bdd/DBUnitRulesBdd.java @@ -12,7 +12,8 @@ "src/test/resources/features/core/core-seed-database.feature", "src/test/resources/features/cdi/cdi-seed-database.feature", "src/test/resources/features/cucumber/cucumber-seed-database.feature", - "src/test/resources/features/general/dataset-replacements.feature" + "src/test/resources/features/general/dataset-replacements.feature", + "src/test/resources/features/general/expected-dataset.feature" }, plugin = "json:target/dbunit-rules.json") public class DBUnitRulesBdd { diff --git a/cdi/src/test/java/com/github/dbunit/rules/bdd/cdi/CDISeedDatabaseStepdefs.java b/cdi/src/test/java/com/github/dbunit/rules/bdd/cdi/CDISeedDatabaseStepdefs.java index 7749660..39d140b 100644 --- a/cdi/src/test/java/com/github/dbunit/rules/bdd/cdi/CDISeedDatabaseStepdefs.java +++ b/cdi/src/test/java/com/github/dbunit/rules/bdd/cdi/CDISeedDatabaseStepdefs.java @@ -1,9 +1,6 @@ package com.github.dbunit.rules.bdd.cdi; -import com.github.dbunit.rules.cdi.api.UsingDataSet; -import com.github.dbunit.rules.model.Tweet; import com.github.dbunit.rules.model.User; -import cucumber.api.PendingException; import cucumber.api.java.en.And; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; @@ -13,8 +10,6 @@ import javax.persistence.EntityManager; import java.util.List; -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -49,17 +44,6 @@ public void The_database_should_be_seeded_with_the_dataset_content() throws Thro } - @Given("^Groovy script engine is on test classpath$") - public void Groovy_script_engine_is_on_test_classpath(String docstring) throws Throwable { - // Express the Regexp above with the code you wish you had - assertNotNull(docstring); - } - - @Then("^Dataset script should be interpreted when seeding the database$") - public void Dataset_script_should_be_interpreted_when_seeding_the_database() throws Throwable { - // Express the Regexp above with the code you wish you had - } - /* Use database seeding in cucumber on cucumber feature @Then("^The database should be seeded with the dataset content before test execution$") diff --git a/cdi/src/test/resources/features/cdi/cdi-seed-database.feature b/cdi/src/test/resources/features/cdi/cdi-seed-database.feature index 81c93e0..da95b28 100644 --- a/cdi/src/test/resources/features/cdi/cdi-seed-database.feature +++ b/cdi/src/test/resources/features/cdi/cdi-seed-database.feature @@ -75,3 +75,5 @@ include::../../src/test/java/com/github/dbunit/rules/DBUnitCDITest.java[tags=see """ Then The database should be seeded with the dataset content before test execution + + diff --git a/cdi/src/test/resources/features/general/dataset-replacements.feature b/cdi/src/test/resources/features/general/dataset-replacements.feature index b568d94..24832c0 100644 --- a/cdi/src/test/resources/features/general/dataset-replacements.feature +++ b/cdi/src/test/resources/features/general/dataset-replacements.feature @@ -45,7 +45,7 @@ include::../../../core/src/test/resources/datasets/yml/groovy-with-date-replacem include::../../../core/src/test/java/com/github/dbunit/rules/ScriptReplacementsIt.java[tags=groovy] ---- """ - Then Dataset script should be interpreted when seeding the database + Then Dataset script should be interpreted while seeding the database Scenario: Seed database with javascript in dataset @@ -70,4 +70,4 @@ include::../../../core/src/test/resources/datasets/yml/js-with-calc-replacements include::../../../core/src/test/java/com/github/dbunit/rules/ScriptReplacementsIt.java[tags=javascript-likes] ---- """ - Then Dataset script should be interpreted when seeding the database \ No newline at end of file + Then Dataset script should be interpreted while seeding the database \ No newline at end of file diff --git a/cdi/src/test/resources/features/general/expected-dataset.feature b/cdi/src/test/resources/features/general/expected-dataset.feature new file mode 100644 index 0000000..0b760c4 --- /dev/null +++ b/cdi/src/test/resources/features/general/expected-dataset.feature @@ -0,0 +1,102 @@ +# order: 5 +Feature: Database assertion using expected datasets +==== +[quote] +____ +In order to verify database state after test execution +As a developer +I want to assert database state with datasets. +____ +==== + + Scenario: Database assertion with yml dataset + + #cukedoctor-discrete + Given The following dataset +""" +---- +include::../../../core/src/test/resources/datasets/yml/expectedUsers.yml[] +---- +""" + +#cukedoctor-discrete + When The following test is executed: +""" +[source,java,indent=0,linenums] +---- +include::../../../core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java[tags=expectedDeclaration;expected] +---- +""" + Then Test must pass because database state is as in expected dataset. + + Scenario: Database assertion with regular expression in expected dataset + + #cukedoctor-discrete + Given The following dataset +""" +---- +include::../../../core/src/test/resources/datasets/yml/expectedUsersRegex.yml[] +---- +""" + + #cukedoctor-discrete + When The following test is executed: +""" +[source,java,indent=0,linenums] +---- +include::../../../core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java[tags=expectedRegex] +---- +""" + Then Test must pass because database state is as in expected dataset. + + Scenario: Database assertion with seeding before test execution + + #cukedoctor-discrete + Given The following dataset +""" +---- +include::../../../core/src/test/resources/datasets/yml/user.yml[] +---- +""" + #cukedoctor-discrete + And The following dataset +""" +---- +include::../../../core/src/test/resources/datasets/yml/expectedUsers.yml[] +---- +""" + + #cukedoctor-discrete + When The following test is executed: +""" +[source,java,indent=0,linenums] +---- +include::../../../core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java[tags=expectedWithSeeding] +---- +""" + Then Test must pass because database state is as in expected dataset. + + Scenario: Failling database assertion + + #cukedoctor-discrete + Given The following dataset +""" +---- +include::../../../core/src/test/resources/datasets/yml/expectedUsers.yml[] +---- +""" + + #cukedoctor-discrete + When The following test is executed: +""" +[source,java,indent=0,linenums] +---- +include::../../../core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java[tags=faillingExpected] +---- +""" + + #cukedoctor-discrete + Then Test must fail showing what it was expecting as database state. +""" +NOTE: This step is failing purposely to show the error message. +""" \ No newline at end of file diff --git a/core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java b/core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java index fc068ca..0780059 100644 --- a/core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java +++ b/core/src/test/java/com/github/dbunit/rules/ExpectedDataSetIt.java @@ -13,6 +13,7 @@ /** * Created by rmpestano on 6/15/16. */ +// tag::expectedDeclaration[] @RunWith(JUnit4.class) public class ExpectedDataSetIt { @@ -23,6 +24,9 @@ public class ExpectedDataSetIt { public DBUnitRule dbUnitRule = DBUnitRule.instance(emProvider.getConnection()); +// end::expectedDeclaration[] + + // tag::expected[] @Test @ExpectedDataSet(value = "yml/expectedUsers.yml",ignoreCols = "id") public void shouldMatchExpectedDataSet() { @@ -34,16 +38,12 @@ public void shouldMatchExpectedDataSet() { emProvider.em().persist(u); emProvider.em().persist(u2); emProvider.tx().commit(); - /* - done by expected dataset - assertThat(u.getId()).isNotNull(); - assertThat(u.getName()).isEqualTo("expected user1"); - assertThat(u2.getId()).isNotNull(); - assertThat(u2.getName()).isEqualTo("expected user2");*/ } + // end::expected[] - @Test @Ignore(value = "How to test failled comparisons?") + // tag::faillingExpected[] + @Test @ExpectedDataSet(value = "yml/expectedUsers.yml",ignoreCols = "id") public void shouldNotMatchExpectedDataSet() { User u = new User(); @@ -55,7 +55,9 @@ public void shouldNotMatchExpectedDataSet() { emProvider.em().persist(u2); emProvider.tx().commit(); } + // end::faillingExpected[] + // tag::expectedRegex[] @Test @ExpectedDataSet(value = "yml/expectedUsersRegex.yml") public void shouldMatchExpectedDataSetUsingRegex() { @@ -68,7 +70,9 @@ public void shouldMatchExpectedDataSetUsingRegex() { emProvider.em().persist(u2); emProvider.tx().commit(); } + // end::expectedRegex[] + // tag::expectedWithSeeding[] @Test @DataSet(value = "yml/user.yml", disableConstraints = true,cleanAfter = true) @ExpectedDataSet(value = "yml/expectedUser.yml", ignoreCols = "id") @@ -77,4 +81,5 @@ public void shouldMatchExpectedDataSetAfterSeedingDataBase() { emProvider.em().remove(emProvider.em().find(User.class,1L)); emProvider.tx().commit(); } + // end::expectedWithSeeding[] }