Spring 当使用自动连线时,Junit测试失败
我正在尝试为我的控制器运行JUnit测试,它有两个自动连接字段。 在运行它时,我在这个对象上得到一个空指针异常,因此无法执行。我对这个春季JUnit测试还不熟悉 我尝试了之前发布的几乎所有与JUNIT相关的组合。它们都不起作用。 helper和model是我的测试中使用的两个自动连接字段。我的测试转到控制器并失败,出现空指针异常。对象助手存在于我的测试类中,但在控制器中为null。 有人能帮我找到解决这个问题的办法吗。下面是我的代码Spring 当使用自动连线时,Junit测试失败,spring,junit4,Spring,Junit4,我正在尝试为我的控制器运行JUnit测试,它有两个自动连接字段。 在运行它时,我在这个对象上得到一个空指针异常,因此无法执行。我对这个春季JUnit测试还不熟悉 我尝试了之前发布的几乎所有与JUNIT相关的组合。它们都不起作用。 helper和model是我的测试中使用的两个自动连接字段。我的测试转到控制器并失败,出现空指针异常。对象助手存在于我的测试类中,但在控制器中为null。 有人能帮我找到解决这个问题的办法吗。下面是我的代码 @RunWith(SpringJUnit4ClassRunne
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "xml/applicationContext.xml"})
@WebAppConfiguration
public class XYZTest {
WebApplicationContext webApplicationContext;
@Autowired
@Qualifier("helper")
Helper helper;
@Autowired
@Qualifier("model")
Model myModel;
DispatcherPortlet dispatcherPortlet;
@Test
public void testInitialize() throws Exception {
DispatcherPortlet portlet = new DispatcherPortlet() {
protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) throws BeansException {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(ExplanationOfBenefitController.class));
wac.refresh();
return wac;
}
};
portlet.init(new MockPortletConfig());
MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW);
MockRenderResponse response = new MockRenderResponse();
//System.out.println("helper:"+helper);
portlet.render(request, response);
assertEquals("tableJSP", response.getContentAsString());
}
}
我可以在控制器中使用JUNIT测试类中创建的自动连线字段吗 为什么你甚至需要助手和模型?两者都没有在你的测试中使用。我已经在我的控制器中使用了它们。我的测试方法调用控制器的initialize方法,该方法使用helper和model。我不知道我是否做对了。。。