使用BeanCreationException
(Spring注释)和@AutoWired
(PowerMock)并运行启用了Spring的TestNG测试时,我得到了@PrepareForTest
。>
我有一个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"); }
我正在尝试在
IWantAHamburgerController
上测试GET方法。这是测试的样子:
@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); }
在测试中,我想监视自动接线的
iWantAHamburgerController
,以验证控制器上的GET方法调用了initGrill
。
如果我删除@PrepareForTest(IWantAHamburgerController.class)
,则不会获得BeanCreationException
,但是PowerMock无法正常工作。
注意:我试图手动设置iWantAHamburgerController
bean,但是当我这样做时,我得到了ClassCastException
。
这里是完整的堆栈:
org.springframework.beans.factory.BeanCreationException:错误用名字创建bean'com.fiveguys.controllers.IWantAHamburgerControllerTest':注入自动关联依赖项失败;嵌套异常为org.springframework.beans.factory.BeanCreationException:无法自动连线栏位:私人com.fiveguys.controllers.IWantAHamburgerControllercom.fiveguys.controllers.IWantAHamburgerControllerTest.iWantAHamburgerController;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:否类型的合格豆找到[com.fiveguys.controllers.IWantAHamburgerController]依赖:预期
[在使用@AutoWired(Spring注释)和@PrepareForTest(PowerMock)并运行启用Spring的TestNG测试时,我得到BeanCreationException。我有一个Spring控制器,通过...
带有powermock的IWantAHamburgerController