Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

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;

@RunWith(SpringJUnit4ClassRunner.class)
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 {

@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();

}

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

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:
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 }}