lordofthejars / nosql-unit

NoSQL Unit is a JUnit extension that helps you write NoSQL unit tests.
Other
383 stars 123 forks source link

Usage with Groovy failed #170

Closed tudvari closed 5 years ago

tudvari commented 7 years ago

Hi,

I would like to ask a little help. I tried to use the nosql-unit with a Groovy and Spock. Unfortunately it doesn't work, could you help me pls? With Java and Junit almost the same code works.

With Java:

public class InMemoTest
{
    @ClassRule public static InMemoryMongoDb inMemoryMongoDb = newInMemoryMongoDbRule().build();
    @Rule public MongoDbRule embeddedMongoDbRule = newMongoDbRule().defaultEmbeddedMongoDb("device1");
    Config config;
    Datastore datastore;

    @Test public void Test1() throws IOException
    {
        DatabaseOperation<MongoClient> dbOps = embeddedMongoDbRule.getDatabaseOperation();
        Morphia morphia = new Morphia();

        datastore = morphia.createDatastore(dbOps.connectionManager(), "device1");
    }
}

With Groovy:

class GetDeviceServiceTest extends Specification {

    static Config config

    @Shared ModelMapper entityMapper
    @Shared Datastore datastore

    @Rule static InMemoryMongoDb inMemoryMongoDb = newInMemoryMongoDbRule().build();
    @Rule public static MongoDbRule embeddedMongoDbRule = newMongoDbRule().defaultEmbeddedMongoDb("device1");

    def setupSpec() {
        DatabaseOperation<MongoClient> dbOps = embeddedMongoDbRule.getDatabaseOperation();

        Morphia morphia = new Morphia();
        datastore = morphia.createDatastore(dbOps.connectionManager(), "device1");
    }

If you try this, I became this error message:

Caused by: java.lang.IllegalStateException: There is no EmbeddedMongo rule with default target defined during test execution. Please create one using @Rule or @ClassRule before executing these tests

Thanks in advance.

BR, Tibor

lordofthejars commented 7 years ago

I am not an expert of Spock but could it be that setupSpec is executed before test event? Can you try https://github.com/lordofthejars/nosql-unit#support-for-jsr-330

tudvari commented 7 years ago

Hi,

I try it a several ways, unfortunately didn't help yet. I became the same error message. Last version of my Test:

static Config config

    @Shared ModelMapper entityMapper
    @Shared Datastore datastore

    @ClassRule  static InMemoryMongoDb inMemoryMongoDb = newInMemoryMongoDbRule().build();
    @Rule static MongoDbRule embeddedMongoDbRule = newMongoDbRule().defaultEmbeddedMongoDb("device1");

    def setupSpec() {
        File configFile = new File(ClassLoader.getSystemResource("test-config.yaml").getFile())

        config = new ConfigReader<Config>().parse(configFile.toURI(), "UTF-8", System.getenv(), Config.class)

        entityMapper = new ModelMapperImpl()

    }

    @UsingDataSet(withSelectiveLocations = @Selective(identifier = "device", locations = ["deviceData.json", "deviceModelData.json", "clientData.json", "vehicleData.json"]), loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
    def "getDevice not found"() {
        given:

        DatabaseOperation<MongoClient> dbOps = embeddedMongoDbRule.getDatabaseOperation();

        Morphia morphia = new Morphia();
        datastore = morphia.createDatastore(dbOps.connectionManager(), "device1");

Caused by: java.lang.IllegalStateException: There is no EmbeddedMongo rule with default target defined during test execution. Please create one using @Rule or @ClassRule before executing these tests.

lordofthejars commented 7 years ago

Ok, I will check on code why this is exception is thrown. Probably it is a problem with Spock integration nor a bug in your test nor in nosqlunit

tudvari commented 7 years ago

Thanks a lot, that would be awesome :)

guima commented 5 years ago

Upgrading spock-core from 1.1-groovy-2.4 to 1.2-groovy-2.4 solved this issue for me.

tudvari commented 5 years ago

Thanks, I would try it tomorrow.

tudvari commented 5 years ago

It's working with the proposal of guima. Thank's for your help.