开发者

Grails. mocked data from a unit test is available in an integration test

Im having a failed integration test because of test pollution (tests pass or fail depending on which order they are run in).

What baffles me a bit however is that it seems that a unit test where i have mocked some data with mockDomain(Media.class,[new Movie(...)]) is still present and available in other tests, even integration tests.

Is this the expected behaviour? why doesn't the test framework clean up after itself for each test?

EDIT

Really strange, the documentation states that:

Integration tests differ from unit tests in that you have full access to the Grails environment within the test. Grails will use an in-memory HSQLDB database for integration tests and clear out all the data f开发者_运维技巧rom the database in between each test.

However in my integration test i have the following code

    protected void setUp() {
      super.setUp()
      assertEquals("TEST POLLUTION!",0,Movie.count())
      ...
    }

Which gives me the output:

TEST POLLUTION! expected:<0> but was:<1>

Meaning that there is data present when there shouldn't be!

Looking at the data that is present int he Movie.list() i find that the data corresponds to data set in a previous test (unit test)

protected void setUp() {
    super.setUp()

    //mock the superclass and subclasses as instances
    mockDomain(Media.class,[
            new Movie(id:1,name:'testMovie')
    ])
    ...
}

Any idea's of why im experiencing these issues?


It's also possible that the pollution is in the test database. Check the DataSources.groovy to see what is being used for the test environment. If you have it set to use a database where dbCreate is set to something other than "create-drop", any previous contents of the database could also be showing up.

If this is the case, the pollution has come from an entirely difference source. Instead of coming from the unit tests, it has actually come from the database, but when switching to run the integration tests you get connected to a real database with all the data it contains.

We experienced this problem, in that our test enviroment was set to have dbCreate as "update". Quite why this was set for integration tests puzzled me, so I switched to use dbCreate as "create-drop" and make sure that when running test suites we started with a clean database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜