Skip to content

Commit e574820

Browse files
committed
Introduce tests for @nested tests in JUnit Jupiter
This commit introduces integration tests that verify the expected behavior for @nested tests in conjunction with the SpringExtension for JUnit Jupiter, including automatic application of TestExecutionListeners to the enclosing test instance of a @nested test instance. Issue: SPR-14150
1 parent ab7b5e5 commit e574820

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2002-2016 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.junit.jupiter.nested;
18+
19+
import org.junit.jupiter.api.Nested;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.test.context.junit.jupiter.SpringExtension;
26+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
27+
import org.springframework.test.context.junit.jupiter.nested.NestedTestsWithSpringAndJUnitJupiterTestCase.TopLevelConfig;
28+
29+
import static org.junit.jupiter.api.Assertions.assertEquals;
30+
31+
/**
32+
* Integration tests that verify support for {@code @Nested} test classes
33+
* in conjunction with the {@link SpringExtension} in a JUnit 5 (Jupiter)
34+
* environment.
35+
*
36+
* @author Sam Brannen
37+
* @since 5.0
38+
* @see org.springframework.test.context.junit4.nested.NestedTestsWithSpringRulesTests
39+
*/
40+
@SpringJUnitConfig(TopLevelConfig.class)
41+
class NestedTestsWithSpringAndJUnitJupiterTestCase {
42+
43+
@Autowired
44+
String foo;
45+
46+
47+
@Test
48+
void topLevelTest() {
49+
assertEquals("foo", foo);
50+
}
51+
52+
53+
@Nested
54+
@SpringJUnitConfig(NestedConfig.class)
55+
class NestedTestCase {
56+
57+
@Autowired
58+
String bar;
59+
60+
61+
@Test
62+
void nestedTest() throws Exception {
63+
// In contrast to nested test classes running in JUnit 4, the foo
64+
// field in the outer instance should have been injected from the
65+
// test ApplicationContext for the outer instance.
66+
assertEquals("foo", foo);
67+
assertEquals("bar", bar);
68+
}
69+
}
70+
71+
// -------------------------------------------------------------------------
72+
73+
@Configuration
74+
public static class TopLevelConfig {
75+
76+
@Bean
77+
String foo() {
78+
return "foo";
79+
}
80+
}
81+
82+
@Configuration
83+
static class NestedConfig {
84+
85+
@Bean
86+
String bar() {
87+
return "bar";
88+
}
89+
}
90+
91+
}

spring-test/src/test/java/org/springframework/test/context/junit4/nested/NestedTestsWithSpringRulesTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*
4040
* @author Sam Brannen
4141
* @since 5.0
42+
* @see org.springframework.test.context.junit.jupiter.nested.NestedTestsWithSpringAndJUnitJupiterTestCase
4243
*/
4344
@RunWith(HierarchicalContextRunner.class)
4445
@ContextConfiguration(classes = TopLevelConfig.class)

0 commit comments

Comments
 (0)