Spring AOP Aspect不使用Mockito

Fab*_*ima 7 junit aop spring mockito

我有一个@Aspect编织所有控制器动作方法的执行.它在我运行系统时工作正常,但在单元测试中没有.我用以下方式使用Mockito和junit:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:**/spring-context.xml")
@WebAppConfiguration
public class UserControllerTest {        
    private MockMvc mockMvc;

    @Mock
    private RoleService roleService;

    @InjectMocks
    private UserController userController;

    @Before
    public void setUp() {
       MockitoAnnotations.initMocks(this);                    
       ...    
       mockMvc = MockMvcBuilders.standaloneSetup(userController).build();
    }    
    ...
}
Run Code Online (Sandbox Code Playgroud)

一些@Test使用mockMvc.perform().

我的看点是:

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() { }

@Pointcut("execution(* mypackage.controller.*Controller.*(..))")
public void methodPointcut() { }

@Around("controller() && methodPointcut()")
...
Run Code Online (Sandbox Code Playgroud)

小智 9

首先,有必要使用webAppContextSetup作为杰森建议:

@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setUp() throws Exception {
   ...
   mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
Run Code Online (Sandbox Code Playgroud)

在这一点上应该触发方面,但Mockito不会注入嘲笑.这是因为Spring AOP使用代理对象,并且正在将mocks注入此代理对象而不是代理对象.要解决这个问题,有必要解开对象并使用ReflectionUtils而不是@InjectMocks注释:

private MockMvc mockMvc;

@Mock
private RoleService roleService;

private UserController userController;

@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setUp() {
   MockitoAnnotations.initMocks(this);                    
   UserController unwrappedController = (UserController) unwrapProxy(userController);
   ReflectionTestUtils.setField(unwrappedController, "roleService", roleService);   
   mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

...

public static final Object unwrapProxy(Object bean) throws Exception {
/*
 * If the given object is a proxy, set the return value as the object
 * being proxied, otherwise return the given object.
 */
    if (AopUtils.isAopProxy(bean) && bean instanceof Advised) {
        Advised advised = (Advised) bean;
        bean = advised.getTargetSource().getTarget();
    }
    return bean;
}
Run Code Online (Sandbox Code Playgroud)

此时,对(...).thenReturn(...)的任何调用应该正常工作.

这里解释:http://kim.saabye-pedersen.org/2012/12/mockito-and-spring-proxies.html

  • 太好了!我使用泛型返回一个类型对象...即`<T> T unwrapProxy(T bean)` (2认同)

Bij*_*men 0

您可能正在使用 Spring AOP,在这种情况下,bean 必须是 Spring bean 才能使 AOP 工作,通过不在控制器中自动装配,它完全绕过了 Spring AOP 机制。

我认为解决方法应该是简单地注入控制器

 @Autowired
 @InjectMocks
 private UserController userController;
Run Code Online (Sandbox Code Playgroud)