use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.
the class ThrowsAdviceInterceptorTests method testCorrectHandlerUsed.
@Test
public void testCorrectHandlerUsed() throws Throwable {
MyThrowsHandler th = new MyThrowsHandler();
ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
FileNotFoundException ex = new FileNotFoundException();
MethodInvocation mi = mock(MethodInvocation.class);
given(mi.getMethod()).willReturn(Object.class.getMethod("hashCode"));
given(mi.getThis()).willReturn(new Object());
given(mi.proceed()).willThrow(ex);
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> ti.invoke(mi)).isSameAs(ex);
assertThat(th.getCalls()).isEqualTo(1);
assertThat(th.getCalls("ioException")).isEqualTo(1);
}
use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.
the class AbstractAopProxyTests method testAddThrowsAdviceWithoutAdvisor.
@Test
public void testAddThrowsAdviceWithoutAdvisor() throws Throwable {
// Reacts to ServletException and RemoteException
MyThrowsHandler th = new MyThrowsHandler();
Echo target = new Echo();
target.setA(16);
ProxyFactory pf = new ProxyFactory(target);
pf.addAdvice(new NopInterceptor());
pf.addAdvice(th);
IEcho proxied = (IEcho) createProxy(pf);
assertThat(th.getCalls()).isEqualTo(0);
assertThat(proxied.getA()).isEqualTo(target.getA());
assertThat(th.getCalls()).isEqualTo(0);
Exception ex = new Exception();
// Will be advised but doesn't match
assertThatExceptionOfType(Exception.class).isThrownBy(() -> proxied.echoException(1, ex)).matches(ex::equals);
// Subclass of RemoteException
MarshalException mex = new MarshalException("");
assertThatExceptionOfType(MarshalException.class).isThrownBy(() -> proxied.echoException(1, mex)).matches(mex::equals);
assertThat(th.getCalls("remoteException")).isEqualTo(1);
}
use of org.springframework.aop.testfixture.advice.MyThrowsHandler in project spring-framework by spring-projects.
the class ProxyFactoryBeanTests method testCanAddThrowsAdviceWithoutAdvisor.
@Test
public void testCanAddThrowsAdviceWithoutAdvisor() throws Throwable {
DefaultListableBeanFactory f = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(f).loadBeanDefinitions(new ClassPathResource(THROWS_ADVICE_CONTEXT, CLASS));
MyThrowsHandler th = (MyThrowsHandler) f.getBean("throwsAdvice");
CountingBeforeAdvice cba = (CountingBeforeAdvice) f.getBean("countingBeforeAdvice");
assertThat(cba.getCalls()).isEqualTo(0);
assertThat(th.getCalls()).isEqualTo(0);
IEcho echo = (IEcho) f.getBean("throwsAdvised");
int i = 12;
echo.setA(i);
assertThat(echo.getA()).isEqualTo(i);
assertThat(cba.getCalls()).isEqualTo(2);
assertThat(th.getCalls()).isEqualTo(0);
Exception expected = new Exception();
assertThatExceptionOfType(Exception.class).isThrownBy(() -> echo.echoException(1, expected)).matches(expected::equals);
// No throws handler method: count should still be 0
assertThat(th.getCalls()).isEqualTo(0);
// Handler knows how to handle this exception
FileNotFoundException expectedFileNotFound = new FileNotFoundException();
assertThatIOException().isThrownBy(() -> echo.echoException(1, expectedFileNotFound)).matches(expectedFileNotFound::equals);
// One match
assertThat(th.getCalls("ioException")).isEqualTo(1);
}
Aggregations