use springBootTest for unit test. The unit test code is as follows. @ SpringBootTest annotation scans all components, including @ Aspect annotated components. Note / / 1 is a service and performs database operations
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CheckCardFormatTest{
@Autowired
private XxxServiceImpl xxxService; // service layer
....
@Test
public void testMainCardFormat() {
String result=xxxService.query("someParam");// 1.
....
}
....
}
the following is the aop code that intercepts all database operations. Note / 2 there is to get the HttpServletRequest, if it works properly in a normal HTTP request. But there is no null pointer exception in the unit test, so I want to @ SpringbootTest annotation scan to eliminate aop, how to solve it?
@Aspect
@Component
public class AbcAspect {
@Around(value = "execution(*com.xx.preparedStatement_execute(..))")
public Object druidIntercept(ProceedingJoinPoint pjp) throws Throwable {
....
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
HttpServletRequest request = servletRequestAttributes.getRequest();// 2.
....
}