<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=david_syer" rel="nofollow">Dave Syer</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-7731?redirect=false" rel="nofollow">SPR-7731</a></strong> and commented</p>
<h4 dir="auto">Overview</h4>
<p dir="auto">Sometimes (always?) it would be nice to have a <code class="notranslate">@Rule</code> implementation that did the job of the <code class="notranslate">SpringJUnit4ClassRunner</code>.</p>
<p dir="auto">The main motivation for this is to be able to use other runners (e.g. <code class="notranslate">@Parameterized</code>, <code class="notranslate">@Categories</code>, Mockito, etc.).</p>
<p dir="auto">There might even be a case for deprecating the runner in favor of a <code class="notranslate">@Rule</code>?</p>
<hr>
<h4 dir="auto">Further Resources</h4>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="655943" data-permission-text="Title is private" data-url="https://github.com/junit-team/junit4/issues/200" data-hovercard-type="issue" data-hovercard-url="/junit-team/junit4/issues/200/hovercard" href="https://github.com/junit-team/junit4/issues/200">junit-team/junit4#200</a></li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="4830351" data-permission-text="Title is private" data-url="https://github.com/junit-team/junit4/issues/444" data-hovercard-type="issue" data-hovercard-url="/junit-team/junit4/issues/444/hovercard" href="https://github.com/junit-team/junit4/issues/444">junit-team/junit4#444</a></li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="22950072" data-permission-text="Title is private" data-url="https://github.com/junit-team/junit4/issues/766" data-hovercard-type="issue" data-hovercard-url="/junit-team/junit4/issues/766/hovercard" href="https://github.com/junit-team/junit4/issues/766">junit-team/junit4#766</a></li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="10326244" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/222" data-hovercard-type="pull_request" data-hovercard-url="/spring-projects/spring-framework/pull/222/hovercard" href="https://github.com/spring-projects/spring-framework/pull/222">#222</a></li>
<li><a href="https://github.com/mmichaelis/spring-aware-rule">https://github.com/mmichaelis/spring-aware-rule</a></li>
</ul>
<hr>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/17657/SpringContextRule.java" rel="nofollow">SpringContextRule.java</a> (<em>4.72 kB</em>)</li>
<li><a href="https://jira.spring.io/secure/attachment/17655/SpringContextRule.java" rel="nofollow">SpringContextRule.java</a> (<em>4.49 kB</em>)</li>
<li><a href="https://jira.spring.io/secure/attachment/22794/spring-test-4.2.0.BUILD-SNAPSHOT.jar" rel="nofollow">spring-test-4.2.0.BUILD-SNAPSHOT.jar</a> (<em>501.52 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398101166" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/11259" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/11259/hovercard" href="https://github.com/spring-projects/spring-framework/issues/11259">#11259</a> Introduce a TestExecutionListener for DbUnit (<em><strong>"Cloned from"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398179752" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/17629" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/17629/hovercard" href="https://github.com/spring-projects/spring-framework/issues/17629">#17629</a> Document SpringClassRule and SpringMethodRule in the reference manual (<em><strong>"is depended on by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398118038" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13870" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13870/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13870">#13870</a> JUnit <code class="notranslate">@Rule</code> executes outside of transaction when using the TransactionalTestExecutionListener (<em><strong>"is depended on by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398156708" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/14850" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/14850/hovercard" href="https://github.com/spring-projects/spring-framework/issues/14850">#14850</a> Implement JUnit 4 Support using Rules (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398091889" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/9965" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/9965/hovercard" href="https://github.com/spring-projects/spring-framework/issues/9965">#9965</a> Add support for parameterized JUnit 4 tests in the Spring TestContext Framework</li>
</ul>
<p dir="auto">25 votes, 30 watchers</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=matthewadams" rel="nofollow">Matthew T. Adams</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-9281?redirect=false" rel="nofollow">SPR-9281</a></strong> and commented</p>
<p dir="auto">I've found a case where the ability to specify that certain bean profiles are active by default is extremely convenient.</p>
<p dir="auto">We are migrating from one environment to another (from pure Hibernate to JPA) in an evolutionary fashion.  When not specifying any active bean profiles, the legacy environment is intended for backward compatibility reasons; when the new environment is desired, only then would the user wish to set the system property "spring.profiles.active" and specify that he wanted to use the "new stuff" (JPA in this case).</p>
<p dir="auto">As an alternative to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398111567" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12852" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12852/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12852">#12852</a>, the choice could be explicitly made by the author of the application context by adding an attribute called "default-profile-activation" similar to the &lt;beans&gt; element.  This would serve to remove any magic.</p>
<p dir="auto">If "default-profile-activation" were an enumerated value, it would make the behavior explicit.  Values I'd recommend are the following.</p>
<ul dir="auto">
<li>
<p dir="auto">"inactive-by-default":  The profile being described is inactive by default and activated only explicitly.  This is the default value if a "profile" attribute is given.  It could be a warning if this value is provided and a nonempty "profile" attribute value were given.</p>
</li>
<li>
<p dir="auto">"active-unless-specified":  The profile being described is active by default, unless other means of explicit activation are provided, like via the "spring.profiles.active" system property.</p>
</li>
<li>
<p dir="auto">"active-always":  The profile being described is always active, regardless of explicit profile activations.  This value is intended for backward compatibility with the &lt;beans&gt; element when it has an empty or no "profile" attribute.  It could be a warning if users provided both a "profile" attribute and a "default-profile-activation" value of "active-always".</p>
</li>
</ul>
<p dir="auto">Consider the following file.</p>
<hr>
<p dir="auto">&lt;beans&gt; &lt;!-- no 'profile' attribute, 'default-profile-activation' value is 'active-always' --&gt;<br>
&lt;bean id="a" class="com.acme.A"/&gt;<br>
&lt;/beans&gt;</p>
<p dir="auto">&lt;beans profile="jpa"&gt; &lt;!-- 'default-profile-activation' value is 'inactive-by-default' --&gt;<br>
&lt;bean id="b" class="com.acme.B"/&gt;<br>
&lt;/beans&gt;</p>
<p dir="auto">&lt;beans profile="pure-hibernate" default-profile-activation="active-unless-specified"&gt;<br>
&lt;bean id="c" class="com.acme.C"/&gt;<br>
&lt;/beans&gt;</p>
<p dir="auto">&lt;beans default-profile-activation="active-always"&gt; &lt;!-- no 'profile' attribute, results in warning since this is the default in this case? --&gt;<br>
&lt;bean id="d" class="com.acme.D"/&gt;<br>
&lt;/beans&gt;</p>
<p dir="auto">&lt;beans profile="jpa" default-profile-activation="inactive-by-default"&gt; &lt;!-- 'profile' attribute given, results in warning since this is the default in this case? --&gt;<br>
&lt;bean id="e" class="com.acme.E"/&gt;<br>
&lt;/beans&gt;</p>
<hr>
<p dir="auto">If the profile "pure-hibernate" or no profiles were explicitly activated, then the beans defined would be [a, c, d].</p>
<p dir="auto">If the profile "jpa" were activated, then the beans defined would be [a, b, d, e].</p>
<p dir="auto">If the profile "foobar", which is not mentioned in the example, were activated, then the beans defined would be [a, d].</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.1.1</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398164076" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/15881" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/15881/hovercard" href="https://github.com/spring-projects/spring-framework/issues/15881">#15881</a> Add explanation of <code class="notranslate">@Profile</code>("default") to reference documentation  (<em><strong>"is depended on by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398111567" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12852" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12852/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12852">#12852</a> Introduce a reserved default profile name (<em><strong>"duplicates"</strong></em>)</li>
</ul>