Spring TestNG PowerMock测试的BeanCreationException

Tho*_*ler 5 testng spring spring-mvc spring-test powermock

我正在BeanCreationException使用@AutoWired(Spring Annotation)和@PrepareForTest(PowerMock)并运行我的Spring启用的TestNG测试.

我有一个Spring控制器,通过组件扫描拾取.

我正在测试TestNG,Mockito和PowerMock.

我试图通过:@AutoWired注释监视自动连接到测试中的自动有线控制器.

这是测试类的开头:

@PowerMockIgnore("*")
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:applicationContext-test.xml" })
@PrepareForTest(IWantAHamburgerController.class)
public class IWantAHamburgerControllerTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;
    @Autowired 
    private MockHttpSession session;
    @Autowired 
    private MockHttpServletRequest request;
    @Autowired
    private IWantAHamburgerController iWantAHamburgerController;

    private MockMvc mockMvc;

    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new org.powermock.modules.testng.PowerMockObjectFactory();
    }

     @BeforeClass
     public void setup() {
         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

         // see the note at the bottom of this post about this line
         //iWantAHamburgerController = (IWantAHamburgerController) applicationContext.getBean("iWantAHamburgerController");
     }
Run Code Online (Sandbox Code Playgroud)

我正在尝试测试GET方法IWantAHamburgerController.这是测试的样子:

    @Test
    public void testGetHamburgerAfterAskingThisQuestion() throws Exception {
        Principal p = PowerMockito.mock(Principal.class);
        PowerMockito.when(p.getName()).thenReturn("oneofthefiveguys");

        IWantAHamburgerController spy = PowerMockito.spy(iWantAHamburgerController);

        PowerMockito.doReturn("oneofthefiveguys").when(spy).getUserName("oneofthefiveguys");

        mockMvc.perform(get("/hamburgers/everythinghamburger.html")).andExpect(status().isOk())
                .andExpect(view().name("jsp/hamburger/everythinghamburger"))
                .andExpect(forwardedUrl("jsp/hamburger/everythinghamburger"));

        PowerMockito.verifyPrivate(spy).invoke("initGrill", "oneofthefiveguys");

        new org.mockito.internal.debugging.MockitoDebuggerImpl().printInvocations(spy);
    }
Run Code Online (Sandbox Code Playgroud)

在测试中,我想监视自动装配iWantAHamburgerController,以验证initGrill控制器上的GET方法是否调用了它.

如果我删除@PrepareForTest(IWantAHamburgerController.class)我没有得到BeanCreationException,但然后PowerMock不起作用.

注意:我试图手动设置iWantAHamburgerControllerbean,但是当我这样做时,我得到了一个ClassCastException.

这是完整的堆栈:

org.springframework.beans.factory.BeanCreationException:创建名为'com.fiveguys.controllers.IWantAHamburgerControllerTest'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.fiveguys.controllers.IWantAHamburgerController com.fiveguys.controllers.IWantAHamburgerControllerTest.iWantAHamburgerController; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[com.fiveguys.controllers.IWantAHamburgerController]的限定bean用于依赖:expected