简体   繁体   中英

FileInputStream can not find file intelliJ idea

I'm writing a DBUnit test. When exporting data from an xml-file, the compiler produces the following error:

java.io.FileNotFoundException: ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/contract/contract-data.xml (No such file or directory).

Here is the corresponding part of the code:

@Before
public void setUp() throws Exception {
    super.setUp();
    beforeData = new FlatXmlDataSet(
            new InputStreamReader(new FileInputStream(
                    "ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/contract/contract-data.xml"),
                    "utf-8"));
    tester.setDataSet(beforeData);
    tester.onSetup();
}

The variable beforeData is of type IDataSet. The Java class resides in the folder named

/src/test/java/ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/ContractDAOImplTest.java

The xml-file resides in the folder named

/src/test/resources/ru.iteco.blockchain.rzd.cred.core.server.db.hibernate.dao.contract/contract-data.xml

I will be very grateful if anyone can explain what the cause of the error is.

"ru/iteco/blockchain/rzd/

This is a relative path, which means it is based on current directory. The error means it does not exist relative to the current directory you run it from.

Best to load resource files from the classpath to eliminate the relative directory problem, eg this.getClass().getResource(filename) .

Consider using FlatXmlDataFileLoader.load(String filename) as it does it correctly for you (that method is in its parent class).

Update:

I did not quite understand what I should use from the proposed one.

If "ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/contract/contract-data.xml" is on the classpath, then

beforeData = new FlatXmlDataFileLoader.load("/ru/iteco/blockchain/rzd/cred/core/server/db/hibernate/dao/contract/contract-data.xml");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM