|
| 1 | +/* |
| 2 | + * Copyright 2013-2021 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.cloud.sleuth.instrument.session; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | + |
| 21 | +import org.aspectj.lang.ProceedingJoinPoint; |
| 22 | +import org.assertj.core.api.BDDAssertions; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.cloud.sleuth.tracer.SimpleTracer; |
| 26 | +import org.springframework.session.SessionRepository; |
| 27 | + |
| 28 | +import static org.mockito.BDDMockito.given; |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | + |
| 31 | +class TraceSessionRepositoryAspectTests { |
| 32 | + |
| 33 | + @Test |
| 34 | + void should_throw_the_cause_of_the_session_exception() { |
| 35 | + SimpleTracer simpleTracer = new SimpleTracer(); |
| 36 | + TraceSessionRepositoryAspect aspect = new TraceSessionRepositoryAspect(simpleTracer, |
| 37 | + simpleTracer.currentTraceContext()) { |
| 38 | + @Override |
| 39 | + Method getMethod(ProceedingJoinPoint pjp, Object tracingWrapper) { |
| 40 | + try { |
| 41 | + return SessionRepository.class.getMethod("createSession"); |
| 42 | + } |
| 43 | + catch (NoSuchMethodException e) { |
| 44 | + throw new RuntimeException(e); |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
| 48 | + ProceedingJoinPoint pjp = mock(ProceedingJoinPoint.class); |
| 49 | + SessionRepository sessionRepository = mock(SessionRepository.class); |
| 50 | + given(pjp.getTarget()).willReturn(sessionRepository); |
| 51 | + given(sessionRepository.createSession()).willThrow(new RuntimeException("Boom!")); |
| 52 | + |
| 53 | + BDDAssertions.thenThrownBy(() -> aspect.wrapSessionRepository(pjp)).isInstanceOf(RuntimeException.class) |
| 54 | + .hasMessageContaining("Boom!"); |
| 55 | + } |
| 56 | + |
| 57 | +} |
0 commit comments