Skip to content

Commit ba6b617

Browse files
committed
Demo: @Autowired rules not supported with Spring's JUnit rules
This commit introduces a test that demonstrates that custom JUnit 4 rules can be @Autowired into a test instance but that they will not be applied by JUnit since JUnit only ever sees such fields as null and therefore ignores them. Issue: SPR-15927
1 parent 699dfc5 commit ba6b617

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2002-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit4.rules;
18+
19+
import org.junit.ClassRule;
20+
import org.junit.Rule;
21+
import org.junit.Test;
22+
import org.junit.rules.TestRule;
23+
import org.junit.runner.Description;
24+
import org.junit.runners.model.Statement;
25+
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
30+
import static org.junit.Assert.*;
31+
32+
/**
33+
* Integration tests for an issue raised in https://jira.spring.io/browse/SPR-15927.
34+
*
35+
* @author Sam Brannen
36+
* @since 5.0
37+
*/
38+
public class AutowiredRuleTests {
39+
40+
@ClassRule
41+
public static final SpringClassRule springClassRule = new SpringClassRule();
42+
43+
@Rule
44+
public final SpringMethodRule springMethodRule = new SpringMethodRule();
45+
46+
@Autowired
47+
@Rule
48+
public AutowiredTestRule autowiredTestRule;
49+
50+
@Test
51+
public void test() {
52+
assertNotNull("TestRule should have been @Autowired", autowiredTestRule);
53+
54+
// Rationale for the following assertion:
55+
//
56+
// The field value for the custom rule is null when JUnit sees it. JUnit then
57+
// ignores the null value, and at a later point in time Spring injects the rule
58+
// from the ApplicationContext and overrides the null field value. But that's too
59+
// late: JUnit never sees the rule supplied by Spring via dependency injection.
60+
assertFalse("@Autowired TestRule should NOT have been applied", autowiredTestRule.applied);
61+
}
62+
63+
@Configuration
64+
static class Config {
65+
66+
@Bean
67+
AutowiredTestRule autowiredTestRule() {
68+
return new AutowiredTestRule();
69+
}
70+
}
71+
72+
static class AutowiredTestRule implements TestRule {
73+
74+
private boolean applied = false;
75+
76+
@Override
77+
public Statement apply(Statement base, Description description) {
78+
this.applied = true;
79+
return base;
80+
}
81+
}
82+
83+
}

0 commit comments

Comments
 (0)