Skip to content

Commit 7618508

Browse files
committed
Verify @⁠MockitoBean can be used with JUnit 4 and SpringMethodRule
See gh-33742
1 parent 2d028c3 commit 7618508

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2002-2024 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+
* https://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.bean.override.mockito.integration;
18+
19+
import org.junit.AfterClass;
20+
import org.junit.BeforeClass;
21+
import org.junit.Rule;
22+
import org.junit.Test;
23+
24+
import org.springframework.test.annotation.Repeat;
25+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
26+
import org.springframework.test.context.junit4.rules.SpringMethodRule;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.BDDMockito.when;
30+
31+
/**
32+
* Tests for {@link MockitoBean @MockitoBean}, {@link SpringMethodRule}, and
33+
* {@link Repeat @Repeat} with JUnit 4.
34+
*
35+
* @author Andy Wilkinson
36+
* @author Sam Brannen
37+
* @see <a href="https://github.com/spring-projects/spring-boot/issues/27693">gh-27693</a>
38+
*/
39+
public class MockitoBeanAndSpringMethodRuleWithRepeatJUnit4IntegrationTests {
40+
41+
private static int invocations;
42+
43+
@Rule
44+
public final SpringMethodRule springMethodRule = new SpringMethodRule();
45+
46+
@MockitoBean
47+
Service service;
48+
49+
@BeforeClass
50+
public static void beforeClass() {
51+
invocations = 0;
52+
}
53+
54+
@Test
55+
@Repeat(2)
56+
public void repeatedTest() {
57+
assertThat(service.greeting()).as("mock should have been reset").isNull();
58+
59+
when(service.greeting()).thenReturn("test");
60+
assertThat(service.greeting()).isEqualTo("test");
61+
62+
invocations++;
63+
}
64+
65+
@AfterClass
66+
public static void afterClass() {
67+
assertThat(invocations).isEqualTo(2);
68+
}
69+
70+
71+
interface Service {
72+
73+
String greeting();
74+
}
75+
76+
}

src/checkstyle/checkstyle-suppressions.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@
109109
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]util[\\/].+Tests" checks="IllegalImport" id="bannedHamcrestImports"/>
110110
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]web[\\/](client|reactive|servlet)[\\/].+Tests" checks="IllegalImport" id="bannedHamcrestImports"/>
111111
<suppress files="src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]test[\\/]context[\\/](aot|junit4)" checks="SpringJUnit5"/>
112-
<suppress files="AutowiredConfigurationErrorsIntegrationTests|ContextHierarchyDirtiesContextTests|ClassLevelDirtiesContextTests|ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests|ContextConfigurationWithPropertiesExtendingPropertiesTests|DirtiesContextInterfaceTests|.+WacTests|JUnit4SpringContextWebTests" checks="SpringJUnit5"/>
113-
<suppress files=".+TestSuite|ContextHierarchyDirtiesContextTests|ClassLevelDirtiesContextTests|ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests|ContextConfigurationWithPropertiesExtendingPropertiesTests|DirtiesContextInterfaceTests|.+WacTests|JUnit4SpringContextWebTests" checks="IllegalImport" id="bannedJUnit4Imports"/>
112+
<suppress files="AutowiredConfigurationErrorsIntegrationTests|ContextHierarchyDirtiesContextTests|ClassLevelDirtiesContextTests|ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests|ContextConfigurationWithPropertiesExtendingPropertiesTests|DirtiesContextInterfaceTests|.+WacTests|JUnit4SpringContextWebTests|MockitoBeanAndSpringMethodRuleWithRepeatJUnit4IntegrationTests" checks="SpringJUnit5"/>
113+
<suppress files=".+TestSuite|ContextHierarchyDirtiesContextTests|ClassLevelDirtiesContextTests|ContextConfigurationWithPropertiesExtendingPropertiesAndInheritedLoaderTests|ContextConfigurationWithPropertiesExtendingPropertiesTests|DirtiesContextInterfaceTests|.+WacTests|JUnit4SpringContextWebTests|MockitoBeanAndSpringMethodRuleWithRepeatJUnit4IntegrationTests" checks="IllegalImport" id="bannedJUnit4Imports"/>
114114
<suppress files="org[\\/]springframework[\\/]test[\\/]context[\\/].+[\\/](ExpectedExceptionSpringRunnerTests|StandardJUnit4FeaturesTests|ProgrammaticTxMgmtTestNGTests)" checks="RegexpSinglelineJava" id="expectedExceptionAnnotation"/>
115115

116116
<!-- spring-web -->

0 commit comments

Comments
 (0)