Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NIO load file in an unit test from src/test/resources

The Problem

I'd like to write a data import in java with java7s NIO. The user enters the path of a file as a String and the programm try to open it by using Paths. When it want to read its DosFileAttributes an java.nio.file.NoSuchFileException: file.txt occures.

What i've found

The only answers i've found till yet is to use an resource Stream - but this seams not practicable, because the file to be loaded is provided by the user and should not be part of the jar. Or did i missunderstood it? http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream%28java.lang.String%29

What i've got

The Setup:

  • Maven 3
  • Java7
  • TestNG
  • Spring

Project Structure:

  • src/main/java - the classes
  • src/test/java - the testcases
  • src/test/resources - perhaps file.txt ? actual it is there

The source that loads the file:

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

@Component( "fileLoader" )
public class BufferdFileLoader
        implements FileLoader {

    @Override
    public List<String> loadFile( String path )
            throws IOException {

        if ( path == null || path.length() == 0 ) {
            throw new IllegalArgumentException( "path should not be null" );
        }

        Path file = Paths.get( path );

        // here the Exception is thrown
        DosFileAttributes attrs = Files.readAttributes( file, DosFileAttributes.class );

        if ( !attrs.isRegularFile() ) {
            throw new IOException( "could not read file, invalid path" );
        }

        List<String> result = new ArrayList<String>();

        try (BufferedReader reader = Files.newBufferedReader( file, Charset.forName( "UTF-8" ) )) {
            String line = null;
            while ( ( line = reader.readLine() ) != null ) {
                result.add( line );
            }
        }

        return result;
    }
}

And the Test case is as simple:

import java.io.IOException;
import java.util.List;

import javax.inject.Inject;

import lombok.Setter;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * Class under test {@link BufferdFileLoader}
 * 
 */
@ContextConfiguration
public class BufferdFileLoaderTest
        extends AbstractTestNGSpringContextTests {

    /**
     * Class under test
     */
    @Inject
    @Setter
    private BufferdFileLoader bufferdFileLoader;


    /**
     * Method under test {@link BufferdFileLoader#loadFile(String)}
     * 
     * @throws IOException
     */
    @Test( groups = { "integration" }, enabled = true )
    public void testImportFeedsFromXMLFileWitEmptyXml()
            throws IOException {

        String filename = "empty-example-takeout.xml";

        List<String> list = this.bufferdFileLoader.loadFile( filename );

        Assert.assertEquals( list.size(), 0 );

    }

}

So my question is

How to load a file using NIO so it is testable with maven and also works in production environment?

like image 657
Thomas Avatar asked Sep 01 '13 10:09

Thomas


1 Answers

Well, it's a matter of obtaining the correct Path for you test resource in the classpath in your test.

EDIT: this is more concise:

Paths.get(getClass().getResource("/"+filename).toURI()).toString()
like image 80
mikołak Avatar answered Oct 02 '22 15:10

mikołak