<?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 &#8211; Zhijun Chen</title>
	<atom:link href="https://zhijunchen.com/category/programming-languages/java/feed/" rel="self" type="application/rss+xml" />
	<link>https://zhijunchen.com</link>
	<description></description>
	<lastBuildDate>Fri, 07 Jun 2024 06:25:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.4</generator>

<image>
	<url>https://zhijunchen.com/wp-content/uploads/2021/03/cropped-my-site-icon-1-32x32.png</url>
	<title>Java &#8211; Zhijun Chen</title>
	<link>https://zhijunchen.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Use Java Annotation With JUnit 5 Extension</title>
		<link>https://zhijunchen.com/use-java-annotation-with-junit-5-extension/</link>
		
		<dc:creator><![CDATA[Zhijun Chen]]></dc:creator>
		<pubDate>Fri, 07 Jun 2024 06:22:38 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<guid isPermaLink="false">https://zhijunchen.com/?p=222</guid>

					<description><![CDATA[Java annotations are quite useful in many areas such as introspection, Aspect-Oriented Programming, etc. Spring Framework uses quite a lot of annotations. My team has been using annotations extensively in our projects and one particular use case is using annotations together with JUnit 5 ParameterResolver to inject test resources. We will start by creating a [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Java annotations are quite useful in many areas such as introspection, Aspect-Oriented Programming, etc. <a href="https://spring.io/projects/spring-framework" target="_blank" rel="noreferrer noopener">Spring Framework</a> uses quite a lot of annotations. My team has been using annotations extensively in our projects and one particular use case is using annotations together with JUnit 5 <a href="https://junit.org/junit5/docs/5.0.2/api/org/junit/jupiter/api/extension/ParameterResolver.html" target="_blank" rel="noreferrer noopener">ParameterResolver</a> to inject test resources.</p>



<p>We will start by creating a custom annotation:</p>



<pre class="wp-block-code"><code lang="java" class="language-java">@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FileResource {
  /**
   * @return the path to the resource.
   */
  String value();

  /**
   * @return the type of value that should be decoded from the resource
   */
  Class&lt;?&gt; type() default String.class;
}</code></pre>



<p>Then we would need to implement a custom ParameterResolver using the annotation.</p>



<pre class="wp-block-code"><code lang="java" class="language-java">public class FileResourceParameterResolver implements ParameterResolver {

  private static final ObjectMapper MAPPER = new ObjectMapper();

  @Override
  public boolean supportsParameter(ParameterContext parameterContext, 
      ExtensionContext extensionContext) throws ParameterResolutionException {
    FileResource fileResource = parameterContext
        .findAnnotation(FileResource.class).orElse(null);
    return fileResource != null &amp;&amp; 
        fileResource.type().isAssignableFrom(
        parameterContext.getParameter().getType());
  }

  @SneakyThrows
  @Override
  public Object resolveParameter(ParameterContext parameterContext, 
      ExtensionContext extensionContext) throws ParameterResolutionException {
    var fileResource = parameterContext
        .findAnnotation(FileResource.class)
        .orElseThrow(IllegalArgumentException::new);
    String path = fileResource.value();
    InputStream content = getClass().getClassLoader().getResourceAsStream(path);
    if (content == null) {
      throw new ParameterResolutionException("Resource not found: " + path);
    }
    if (fileResource.type() == String.class) {
      return IOUtils.toString(content, StandardCharsets.UTF_8);
    }
    return MAPPER.readValue(content, parameterContext.getParameter().getType());
  }
}</code></pre>



<p>Now we have our custom ParameterResolver ready to use. In our test, what we need to do is the following:</p>



<pre class="wp-block-code"><code lang="java" class="language-java">@ExtendWith({ FileResourceParameterResolver.class })
class SomeTest {

  @Test
  void importResourceTest(@FileResource("some/json/file.json") String inputJson) {
    // use content below in test
  }
}</code></pre>



<p></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
