Assert Null and Aspect

来源:互联网 发布:单片机新颖毕业设计 编辑:程序博客网 时间:2024/06/05 02:57

When apply the handy Assert.notNull, we should be aware that it might throw IllegalArgumentException.
@Aspect@Componentpublic class ExceptionAspect {private final Logger log = LoggerFactory.getLogger(getClass());@AfterThrowing(throwing = "ex", pointcut = "within(com.xxx.broker..*)")public void catchIllegalArgumentException(Throwable ex) {log.error("IllegalArgumentException occurs! ", ex);}}
Test:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath*:applicationContext.xml")@ActiveProfiles("test")public class ExceptionAspectTest {@Autowiredprivate IBrokerService brokerService;@Testpublic void testInputNull(){brokerService.getAllHoldings(null);}}
Service
@Overridepublic Response getAllHoldings(Request request) {Assert.notNull(request);Response response = new Response();...return response;}
Application context:
<aop:aspectj-autoproxy />

POM.xml

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>${aspectj.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${springframework.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>${aspectj.version}</version></dependency>










0 0