<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java 7 &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/java-7/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 18 Jun 2022 14:02:57 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>How to load files from Spring JUnit test resources folder</title>
		<link>https://codepills.com/how-to-load-files-from-spring-junit-test-resources-folder/</link>
					<comments>https://codepills.com/how-to-load-files-from-spring-junit-test-resources-folder/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Wed, 03 Mar 2021 10:04:00 +0000</pubDate>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ClassLoader]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Java 7]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[legacy project]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[testing]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1141</guid>

					<description><![CDATA[In this short tutorial, you will see how to read the files from the <i>/src/test/resources</i> test directory path in your Spring application. <a href="https://codepills.com/how-to-load-files-from-spring-junit-test-resources-folder/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>In this short tutorial we will show you how to read the files from the <i>/src/test/resources</i> directory path.</p>
<p><span id="more-1141"></span></p>
<p>It is common practice for unit testing in the Spring JUnit test to load files and their content to make more robust test cases. Or we have a test case where we need perhaps read or write data or test specific files in general.</p>
<h2>1. Using ClassLoader</h2>
<p>First option is to use and instance of the <code>ClassLoader</code>.</p>
<pre><code class="language-java">final ClassLoader classLoader = getClass().getClassLoader();
final String resourceName = "daily_report.csv";
final File file = new File(classLoader.getResource(resourceName).getFile());
final String absolutePath = file.getAbsolutePath();

System.out.println(absolutePath);

Assert.assertTrue(absolutePath.endsWith("/daily_report.csv"));</code></pre>
<p>We will get the output of the absolute path as:</p>
<pre><code class="language-bash">/Users/your.user.name/DEV/tutorials/testing-modules/junit-5-basics/target/test-classes/daily_report.csv</code></pre>
<p>Be aware that the path to the resource will look slightly different from the rest of the examples. It is because the ClassLoader looks for the resources on the classpath. Therefore obtaining a path to the resource is from the point of the compiled project.</p>
<p>If you build your project with Maven, compiled classes and resources are in the <code>/target</code> directory in the project root folder. That is why you need to count on the path to resource adjusted for the difference. The other two methods use project root.</p>
<h2>2. Using Path</h2>
<p>Second option is to use an instance of the <code>Path</code> class. We will call a static factory method <code>Paths.get()</code> which will return us its new instance. Then we will convert <code>Path</code> to <code>File</code> instance and from the that we will extract absolute path with <code>getAbsolutePath()</code> method.</p>
<pre><code class="language-java">final Path resourcePath = Paths.get("src","test","resources");
final String absolutePath = resourcePath.toFile().getAbsolutePath();

System.out.println(absolutePath);

Assert.assertTrue(absolutePath.endsWith("src/test/resources"));</code></pre>
<p>We will get the same output as in example using <code>java.io.File</code>:</p>
<pre><code class="language-bash">/Users/your.user.name/DEV/tutorials/testing-modules/junit-5-basics/src/test/resources</code></pre>
<p><i>Path</i> class was introduced in Java 7; consequently, it is available also in older and legacy projects.</p>
<h2>3. Using java.io.File</h2>
<p>Third approach is to use an instance of the <code>java.io.File</code> class to read the <i>/src/test/resources</i> directory. We will create instance and then we will call its method <code>getAbsolutePath()</code> method. Path we will obtain in this way is relative path towards the current working directory of the project.</p>
<pre><code class="language-java">final String resourcePath = "src/test/resources";
final File file = new File(resourcePath);
final String absolutePath = file.getAbsolutePath();

System.out.println(absolutePath);

Assert.assertTrue(absolutePath.endsWith("src/test/resources"));</code></pre>
<p>Let&#8217;s see an example output when running on the Unix based system:</p>
<pre><code class="language-bash">/Users/your.user.name/DEV/tutorials/testing-modules/junit-5-basics/src/test/resources</code></pre>
<h2>Conclusion</h2>
<p>This article was a quick tutorial where we showed how to read resources from the <i>/src/test/resources</i> directory in JUnit 5.</p>
<p>There are three different ways how to get a resource for testing. We can either use File, Paths or ClassLoader class.</p>
<p>As always, you can find all our examples on our <a href="https://github.com/codekopf/tutorials-jvm/tree/master/testing-modules" title="Tutorial JVM - Testing modules" target="_blank" rel="nofollow noopener">GitHub project</a>!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/how-to-load-files-from-spring-junit-test-resources-folder/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
