Over a million developers have joined DZone.

New Rules Have Arrived in Spring Framework 4.2.0

DZone's Guide to

New Rules Have Arrived in Spring Framework 4.2.0

Now that Spring Framework 4.2.0.RELEASE has launched, there are two new rules - SpringClassRule and SpringMethodRule.

· Java Zone ·
Free Resource

Learn how to stop testing everything every sprint and only test the code you’ve changed. Brought to you by Parasoft.

Spring Framework 4.2.0.RELEASE has arrived and two new rules have been added to the spring-framework project: SpringClassRule and SpringMethodRule.

In Spring applications we have used SpringJUnit4ClassRunner for a long time in our tests but JUnit only supported one Runner at the time, in order to allow integration between Spring with other Runners like org.junit.runners.Parameterized. Now we can use these new classes instead of @RunWith(SpringJUnit4ClassRunner.class)

  • SpringClassRule enables the following annotations @BeforeClass, @AfterClass, @ProfileValueSourceConfiguration@IfProfileValue.

  • SpringMethodRule enables the following annotations @Before, @After, @Repeat, @Timeout, @ProfileValueSourceConfiguration, @IfProfileValue.

Test using classic SpringJUnit4ClassRunner:

import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

public class SpringTest {


Test using new rules SpringClassRule and SpringMethodRule:

import org.junit.ClassRule;
import org.junit.Rule;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;

public class SpringTest {

public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

public final SpringMethodRule springMethodRule = new SpringMethodRule();


Both have the same effect in your test and you can use them in different contexts.

Get the top tips for Java developers and best practices to overcome common challenges. Brought to you by Parasoft.

spring framework ,junit ,java

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}