|
| 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 | +} |
0 commit comments