|
| 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.easymock; |
| 18 | + |
| 19 | +import org.easymock.EasyMockSupport; |
| 20 | +import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
| 21 | +import org.junit.jupiter.api.Order; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.TestMethodOrder; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.ApplicationContext; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.test.context.TestExecutionListeners; |
| 30 | +import org.springframework.test.context.bean.override.example.ExampleService; |
| 31 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.easymock.EasyMock.expect; |
| 35 | +import static org.easymock.EasyMock.replay; |
| 36 | +import static org.easymock.EasyMock.reset; |
| 37 | +import static org.springframework.test.context.TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS; |
| 38 | + |
| 39 | +/** |
| 40 | + * Integration tests for {@link EasyMockBean @EasyMockBean}. |
| 41 | + * |
| 42 | + * @author Sam Brannen |
| 43 | + * @since 6.2 |
| 44 | + */ |
| 45 | +@SpringJUnitConfig |
| 46 | +@TestExecutionListeners(listeners = EasyMockResetTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS) |
| 47 | +@TestMethodOrder(OrderAnnotation.class) |
| 48 | +public class EasyMockBeanIntegrationTests { |
| 49 | + |
| 50 | + @Autowired |
| 51 | + ApplicationContext ctx; |
| 52 | + |
| 53 | + @EasyMockBean |
| 54 | + ExampleService service; |
| 55 | + |
| 56 | + @Test |
| 57 | + @Order(1) |
| 58 | + void test1() { |
| 59 | + assertThat(ctx.getBean("service", ExampleService.class)) |
| 60 | + .satisfies(this::assertIsEasyMock) |
| 61 | + .isSameAs(service); |
| 62 | + |
| 63 | + // Before mock setup |
| 64 | + assertThat(service.greeting()).isNull(); |
| 65 | + reset(service); |
| 66 | + |
| 67 | + // After mock setup |
| 68 | + expect(service.greeting()).andReturn("mocked"); |
| 69 | + replay(service); |
| 70 | + assertThat(service.greeting()).isEqualTo("mocked"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + @Order(2) |
| 75 | + void test2() { |
| 76 | + assertThat(ctx.getBean("service", ExampleService.class)) |
| 77 | + .satisfies(this::assertIsEasyMock) |
| 78 | + .isSameAs(service); |
| 79 | + |
| 80 | + // Before mock setup |
| 81 | + assertThat(service.greeting()).isNull(); |
| 82 | + reset(service); |
| 83 | + |
| 84 | + // After mock setup |
| 85 | + expect(service.greeting()).andReturn("mocked"); |
| 86 | + replay(service); |
| 87 | + assertThat(service.greeting()).isEqualTo("mocked"); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + private void assertIsEasyMock(Object obj) { |
| 92 | + assertThat(EasyMockSupport.isAMock(obj)).as("is EasyMock mock").isTrue(); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + @Configuration(proxyBeanMethods = false) |
| 97 | + static class Config { |
| 98 | + |
| 99 | + @Bean |
| 100 | + ExampleService service() { |
| 101 | + return () -> "enigma"; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments