I'd already tried with this stack overflow question but I'm a little bit lost with maven.
In a Maven project I want to test a function which finally writes a text file in the given path. The signature of my function is boolean printToFile(String absolutePath)
(the returning value is a success flag)
Under src/test/resources
I have my expected file; lets call it expected.txt
.
Using the apache.commons.commons-io
dependency:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
I want to call my function; create two File
objects and compare their content:
@Test
public void fileCreationTest() {
String outputPath = Thread.currentThread().getClass().getClassLoader().getResource("got.txt").getFile();
myTestedObject.printToFile(outputPath);
File got = new File(outputPath);
String expectedFilePath = Thread.currentThread().getClass().getClassLoader().getResource("expected.txt").getFile();
File expected = new File(expectedFilePath)
boolean areEqual = FileUtils.contentEquals(got, expected);
Assert.assertTrue(areEqual);
[EDITED]
It's not a matter of the calling function: If I call it from normal code, it does work But If I run my test, it fails (from maven or from my IDE). I think it's something related with the test nature.