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

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

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.

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
spring framework ,junit ,java

Published at DZone with permission of Eddú Meléndez. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}