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

夙愿已清 提交于 2019-12-10 11:48:04

问题


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 from 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?


回答1:


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.



来源:https://stackoverflow.com/questions/6080568/grails-mocked-data-from-a-unit-test-is-available-in-an-integration-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!