fakemongo / fongo

faked out in-memory mongo for java
Apache License 2.0
523 stars 156 forks source link

Unit Test using TestNG #295

Closed yesid-bocanegra closed 7 years ago

yesid-bocanegra commented 7 years ago

reading at fongo documentation i found that it supports the @Rule annotation to delete the contents of the DB before executing a test.

i want to achieve the same thing using TestNG but i can't find the correct way to do this, i tried calling fongo.dropDatabase(dbName) but it doesn't work as expected, if i call this method it seems that DB is droped but no new instance is created when inserting a new document.

also tried to delete and instance again fongo, but also no results. its the same i can create or update documents but if i try to fetch a document there will be no results.

to delete contents of fongo db i'm using @BeforeMethod annotation and there call to dropDatabase().

how should i delete db before every test?

twillouer commented 7 years ago

Hi,

FongoRule permit to create one database. So, in the "@after", the database is dropped. Each run of a junit test create a new object "Fongo", so you're sure to be "clean" at each "@Test".

I don't know TestNG, but JUnit run the "@Rule" at each test method. You start with a fresh database, and you remove it just after the test is finished.

yesid-bocanegra commented 7 years ago

i've found no way to achieve the same behavior using TestNG, i had to change the scope of my beans in test environment, then exposed the dropdatabase from Fongo, so i drop and recreate beans for every test, i know its the same thing as a rule but it involves several modifications to get it right.

i think there is nothing like @Rule for testng in fongo but it should be a great addition