<?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>Junit &#8211; Pauls Blog</title>
	<atom:link href="https://sterl.org/tag/junit/feed/" rel="self" type="application/rss+xml" />
	<link>https://sterl.org</link>
	<description></description>
	<lastBuildDate>Sat, 21 Sep 2019 12:58:14 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Use Session and Request Scope Beans in Tests with Spring</title>
		<link>https://sterl.org/2015/07/use-session-and-request-scope-bean-in-tests-with-spring/</link>
					<comments>https://sterl.org/2015/07/use-session-and-request-scope-bean-in-tests-with-spring/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Fri, 10 Jul 2015 18:00:25 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Junit]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Unit Test]]></category>
		<guid isPermaLink="false">http://sterl.org/?p=53</guid>

					<description><![CDATA[Sometimes it is useful haven the Web Scopes in Unit Tests. Usually Spring just show this very useful error in the Unit Test: Caused by: java.lang.IllegalStateException: No Scope registered for scope 'session' at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) ... 28 common frames omitted You can easily have all needed&#8230;]]></description>
										<content:encoded><![CDATA[<p>Sometimes it is useful haven the Web Scopes in Unit Tests. Usually Spring just show this very useful error in the Unit Test:</p>
<pre class="lang:default decode:true " title="Error Missing Web Scopes">Caused by: java.lang.IllegalStateException: No Scope registered for scope 'session'
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
	... 28 common frames omitted
</pre>
<p>You can easily have all needed web scopes in your unit test by adding the following annotation to your Tests: <code>@WebAppConfiguration</code>.</p>
<p>Now Web Scopes like:</p>
<pre class="lang:java decode:true " title="Using Scope in the Spring Application Configuration">@Bean(name = "foo") @Scope("session")
public String getString()</pre>
<p>will work.</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2015/07/use-session-and-request-scope-bean-in-tests-with-spring/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Spring Boot Configuration &#8211; Properties not loaded</title>
		<link>https://sterl.org/2015/07/spring-boot-configuration-properties-not-loaded/</link>
					<comments>https://sterl.org/2015/07/spring-boot-configuration-properties-not-loaded/#respond</comments>
		
		<dc:creator><![CDATA[Paul Sterl]]></dc:creator>
		<pubDate>Fri, 10 Jul 2015 17:29:05 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[ContextConfiguration]]></category>
		<category><![CDATA[Junit]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[SpringApplicationConfiguration]]></category>
		<category><![CDATA[Unit Test]]></category>
		<guid isPermaLink="false">http://sterl.org/?p=47</guid>

					<description><![CDATA[I had recently a very interesting problem using Spring Boot and the automagic concerning the configuration. The Problem org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demo.MyApplicationTests': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String demo.MyApplicationTests.url; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'my.url' in string value "${my.url}" at&#8230;]]></description>
										<content:encoded><![CDATA[
<p> I had recently a very interesting problem using Spring Boot and the automagic concerning the configuration. </p>



<h2 class="wp-block-heading">The Problem</h2>



<pre>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demo.MyApplicationTests': Injection of autowired dependencies failed; 
	nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String demo.MyApplicationTests.url; 
	nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'my.url' in string value "${my.url}" at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
		~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE] 
	Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'my.url' in string value "${my.url}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) 
		~[spring-core-4.1.7.RELEASE.jar:4.1.7.RELEASE]
</pre>



<p>Configuration was not available in my Unit tests but it was working fine in the normal application. I spend a lot of time searching but without real good post concerning this issue.</p>



<p>This code already wasn&#8217;t working:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-eclipse" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}">@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyWebappApplication.class)
@WebAppConfiguration
public class MyApplicationTests {
    @Value(&quot;${my.url}&quot;)
    String url;

    @Test
    public void test() {
        System.out.println(url);
    }
}</pre></div>



<p>Even if my <code>application.yml</code> / <code>application.properties</code> was looking good:</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-material" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;yaml&quot;,&quot;mime&quot;:&quot;text/yaml&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;YAML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;yaml&quot;}">my:
   url: any kind of String we inject</pre></div>



<h2 class="wp-block-heading">The solution</h2>



<p>Now here the working sample, do you see the difference?</p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-eclipse" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}">@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyWebappApplication.class)
@WebAppConfiguration
public class MyApplicationTests {
    @Value(&quot;${my.url}&quot;)
    String url;

    @Test
    public void test() {
        System.out.println(url);
    }
}</pre></div>



<p>Yes it is just the <code>@SpringApplicationConfiguration</code> instead of the <code>@ContextConfiguration</code> annotation. (Even if the first is itself just a meta-annotation of the second.) If you look into this new annotation you will find that it uses a different loader class, which makes the difference.  </p>



<div class="wp-block-codemirror-blocks-code-block code-block"><pre class="CodeMirror cm-s-eclipse" data-setting="{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text/x-java&quot;,&quot;theme&quot;:&quot;eclipse&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Java&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;java&quot;}">@ContextConfiguration(loader = SpringApplicationContextLoader.class)
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SpringApplicationConfiguration</pre></div>



<p>That&#8217;s the trick. Having it in place the Spring Application configuration is successfully parsed an available in your unit test. Doesn&#8217;t matter if <code>YAML</code> or just <code>property</code> file. </p>
]]></content:encoded>
					
					<wfw:commentRss>https://sterl.org/2015/07/spring-boot-configuration-properties-not-loaded/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
