|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 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 | +package org.springframework.data.redis.core; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 20 | +import static org.mockito.Mockito.doReturn; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.spy; |
| 23 | +import static org.mockito.Mockito.times; |
| 24 | +import static org.mockito.Mockito.verify; |
| 25 | +import static org.mockito.Mockito.verifyNoInteractions; |
| 26 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 27 | + |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import org.springframework.data.redis.connection.RedisConnectionFactory; |
| 31 | + |
| 32 | +/** |
| 33 | + * Unit Tests for {@link RedisAccessor}. |
| 34 | + * |
| 35 | + * @author John Blum |
| 36 | + * @see org.junit.jupiter.api.Test |
| 37 | + * @see org.springframework.data.redis.core.RedisAccessor |
| 38 | + * @since 3.2.0 |
| 39 | + */ |
| 40 | +public class RedisAccessorUnitTests { |
| 41 | + |
| 42 | + @Test |
| 43 | + public void setAndGetConnectionFactory() { |
| 44 | + |
| 45 | + RedisConnectionFactory mockConnectionFactory = mock(RedisConnectionFactory.class); |
| 46 | + |
| 47 | + RedisAccessor redisAccessor = new RedisAccessor(); |
| 48 | + |
| 49 | + assertThat(redisAccessor.getConnectionFactory()).isNull(); |
| 50 | + |
| 51 | + redisAccessor.setConnectionFactory(mockConnectionFactory); |
| 52 | + |
| 53 | + assertThat(redisAccessor.getConnectionFactory()).isSameAs(mockConnectionFactory); |
| 54 | + assertThat(redisAccessor.getRequiredConnectionFactory()).isSameAs(mockConnectionFactory); |
| 55 | + |
| 56 | + redisAccessor.setConnectionFactory(null); |
| 57 | + |
| 58 | + assertThat(redisAccessor.getConnectionFactory()).isNull(); |
| 59 | + |
| 60 | + verifyNoInteractions(mockConnectionFactory); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void getRequiredConnectionFactoryWhenNull() { |
| 65 | + |
| 66 | + assertThatIllegalStateException() |
| 67 | + .isThrownBy(() -> new RedisAccessor().getRequiredConnectionFactory()) |
| 68 | + .withMessage("RedisConnectionFactory is required") |
| 69 | + .withNoCause(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void afterPropertiesSetCallsGetRequiredConnectionFactory() { |
| 74 | + |
| 75 | + RedisConnectionFactory mockConnectionFactory = mock(RedisConnectionFactory.class); |
| 76 | + |
| 77 | + RedisAccessor redisAccessor = spy(new RedisAccessor()); |
| 78 | + |
| 79 | + doReturn(mockConnectionFactory).when(redisAccessor).getRequiredConnectionFactory(); |
| 80 | + |
| 81 | + redisAccessor.afterPropertiesSet(); |
| 82 | + |
| 83 | + verify(redisAccessor, times(1)).afterPropertiesSet(); |
| 84 | + verify(redisAccessor, times(1)).getRequiredConnectionFactory(); |
| 85 | + verifyNoMoreInteractions(redisAccessor); |
| 86 | + } |
| 87 | +} |
0 commit comments