Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to get a test file into JUnit

Can somebody suggest an easy way to get a reference to a file as a String/InputStream/File/etc type object in a junit test class? Obviously I could paste the file (xml in this case) in as a giant String or read it in as a file but is there a shortcut specific to Junit like this?

public class MyTestClass{  @Resource(path="something.xml") File myTestFile;  @Test public void toSomeTest(){ ... }  } 
like image 527
benstpierre Avatar asked Apr 08 '10 02:04

benstpierre


People also ask

How do I add a test File to JUnit?

To use JUnit you must create a separate . java file in your project that will test one of your existing classes. In the Package Explorer area on the left side of the Eclipse window, right-click the class you want to test and click New → JUnit Test Case.

How do I read a JUnit File?

Read File and Resource in JUnit Test Examples Any file under src/test/resources is often copied to target/test-classes. To access these resource files in JUnit we can use the class's resource. It will locate the file in the test's classpath /target/test-classes.

How do I run a JUnit test case from a jar File?

We can run a JUnit 5 test case using JUnit's console launcher. The executable for this jar can be downloaded from Maven Central, under the junit-platform-console-standalone directory. Let's see how we can run different test cases using the console launcher.


2 Answers

You can try @Rule annotation. Here is the example from the docs:

public static class UsesExternalResource {     Server myServer = new Server();      @Rule public ExternalResource resource = new ExternalResource() {         @Override         protected void before() throws Throwable {             myServer.connect();         };          @Override         protected void after() {             myServer.disconnect();         };     };      @Test public void testFoo() {         new Client().run(myServer);     } } 

You just need to create FileResource class extending ExternalResource.

Full Example

import static org.junit.Assert.*;  import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExternalResource;  public class TestSomething {     @Rule     public ResourceFile res = new ResourceFile("/res.txt");      @Test     public void test() throws Exception     {         assertTrue(res.getContent().length() > 0);         assertTrue(res.getFile().exists());     } } 

import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset;  import org.junit.rules.ExternalResource;  public class ResourceFile extends ExternalResource {     String res;     File file = null;     InputStream stream;      public ResourceFile(String res)     {         this.res = res;     }      public File getFile() throws IOException     {         if (file == null)         {             createFile();         }         return file;     }      public InputStream getInputStream()     {         return stream;     }      public InputStream createInputStream()     {         return getClass().getResourceAsStream(res);     }      public String getContent() throws IOException     {         return getContent("utf-8");     }      public String getContent(String charSet) throws IOException     {         InputStreamReader reader = new InputStreamReader(createInputStream(),             Charset.forName(charSet));         char[] tmp = new char[4096];         StringBuilder b = new StringBuilder();         try         {             while (true)             {                 int len = reader.read(tmp);                 if (len < 0)                 {                     break;                 }                 b.append(tmp, 0, len);             }             reader.close();         }         finally         {             reader.close();         }         return b.toString();     }      @Override     protected void before() throws Throwable     {         super.before();         stream = getClass().getResourceAsStream(res);     }      @Override     protected void after()     {         try         {             stream.close();         }         catch (IOException e)         {             // ignore         }         if (file != null)         {             file.delete();         }         super.after();     }      private void createFile() throws IOException     {         file = new File(".",res);         InputStream stream = getClass().getResourceAsStream(res);         try         {             file.createNewFile();             FileOutputStream ostream = null;             try             {                 ostream = new FileOutputStream(file);                 byte[] buffer = new byte[4096];                 while (true)                 {                     int len = stream.read(buffer);                     if (len < 0)                     {                         break;                     }                     ostream.write(buffer, 0, len);                 }             }             finally             {                 if (ostream != null)                 {                     ostream.close();                 }             }         }         finally         {             stream.close();         }     }  } 

like image 100
Ha. Avatar answered Oct 13 '22 05:10

Ha.


If you need to actually get a File object, you could do the following:

URL url = this.getClass().getResource("/test.wsdl"); File testWsdl = new File(url.getFile()); 

Which has the benefit of working cross platform, as described in this blog post.

like image 32
slashnick Avatar answered Oct 13 '22 05:10

slashnick