How to prevent refactor in tests with `moveFile` method

Hi all.
I’m having an issue when writing tests.
I need some assets to be able to run my tests. In the setup method,
i copy an assets directory in my test project and then move my test file inside this directory.
But when doing so (moving the file), the ide seems to prerform refactoring even though it completely breaks my tests.

This is the file before


<screens xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://ofbiz.apache.org/Widget-Screen"
         xsi:schemaLocation="http://ofbiz.apache.org/Widget-Screen http://ofbiz.apache.org/dtds/widget-screen.xsd">
    <screen name="Foo">
        <section>
            <widgets>
                <include-screen name="DuplicatedTargetScreen" location="component://zelda/widget/DuplicatedTargetScreens.xml"/>
            </widgets>
        </section>
    </screen>
</screens>

This is the test code

void testDuplicatedTargetScreen() {
    String desc = message('inspection.screen.duplicate.display.descriptor')
    myFixture.enableInspections(new DuplicatedScreenInspection())
    String file = "${this.getTestName(false)}.xml"
    String dest = 'zelda/widget'
    myFixture.moveFile("xml/$file", dest)
    myFixture.configureByFile("$dest/$file")
    doHighlight(true, desc)
}

protected void doHighlight(boolean mustFind, String desc) {
    List<HighlightInfo> highlightInfos = myFixture.doHighlighting()
    List<String> highlightDescs = highlightInfos.collect { it.description }
    if (!mustFind) {
        assert !highlightDescs.contains(desc)
    } else {
        assertFalse highlightInfos.isEmpty()
        assert highlightDescs.contains(desc)
    }
}

And this is the test file after (I used debug to get the file content)

<screens xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://ofbiz.apache.org/Widget-Screen"
         xsi:schemaLocation="http://ofbiz.apache.org/Widget-Screen http://ofbiz.apache.org/dtds/widget-screen.xsd">

    <screen name="Foo">
        <section>
            <widgets>
                <include-screen name="DuplicatedTargetScreen" location="../../elden/widget/EldenScreens.xml"/>
            </widgets>
        </section>
    </screen>
</screens>

Notice the location attribute value that has been modified.
Is there a way to prevent refactoring in tests while moving files ? It kinda kills the tests
Thanks

  1. Parametrize setup() and copy files to expected location for each test. You can use custom annotation for each test or use test name, for example.
  2. perform such setup in each test method with dedicated path “manually”

I already do such configs in setup. But idealy all my test files (with in) are stored in a consistant way. Considering most tests do not need to do a move it breaks the logic.
That’s why i didn’t thought about copying the file the test is run against in the setup.
But that should work thanks :slight_smile:

1 Like