junit测试用例给出错误

时间:2013-10-23 06:53:43

标签: spring-mvc junit spring-annotations

我是编写测试用例的新手:

我的控制器如下:

 @RequestMapping(value={"/addschool" }, method = RequestMethod.GET)
        public  final ModelAndView  addschool(@ModelAttribute("addschool") Pricing pricing, Map<String, Object> map,
                 Model model,  HttpServletRequest request) {

               System.out.println("Name=" + pricing.getName() + " age=" + request.getParameter("age"));

               ModelAndView mv = new ModelAndView(ADDSCHOOL);
                return mv;
           }

我的junit测试用例是这样的:

public class AddSchoolTest {

    @Autowired
    private UserManagementController controller;

    @Test
    public void testAddSchool() {

        ModelMap model = new ModelMap();
        HttpServletRequest request = new MockHttpServletRequest();
        Pricing pricing = new Pricing();
        String userName = "Laily";
        pricing.setName(userName);
        //ModelAndView mav= controller.handleRequest();
        try{
        ModelAndView mav= controller.addschool(pricing, model,null, request);
        //fail("Not yet implemented");
        assertNull(pricing);
        assertFalse(model.isEmpty());
        Assert.assertEquals("addschool", mav.getViewName());
        }catch(Exception e){

        }
    }

}

但我的问题是,即使我将删除“添加学校”并将其他东西显示为绿色,但如果我删除尝试和捕获,而不是在每种情况下都会给出红色。问题是什么。

完整堆栈跟踪就是这个

Error creating bean with name 'com.enbee.admin.controller.AddSchoolTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.enbee.admin.controller.AddInstiDeptLibController com.enbee.admin.controller.AddSchoolTest.controller; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.enbee.admin.controller.AddInstiDeptLibController] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)

aused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.enbee.admin.controller.AddInstiDeptLibController com.enbee.admin.controller.AddSchoolTest.controller; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.enbee.admin.controller.AddInstiDeptLibController] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 26 more

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.enbee.admin.controller.AddInstiDeptLibController] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    ... 28 more

3 个答案:

答案 0 :(得分:0)

您可以尝试更改

ModelAndView mv = new ModelAndView(ADDSCHOOL);

ModelAndView mv = new ModelAndView(addschool);

ModelAndView mv = new ModelAndView(pricing);

就像在@ModelAttribute("addschool") Pricing pricing中一样 在你的控制器动作中。

我不确定但是能看出它是否有效

感谢。

答案 1 :(得分:0)

如果它是红色然后您的测试用例正在下降,您的catch块为空,打印堆栈跟踪并查看问题是什么,例外情况下您的测试用例将失败。

答案 2 :(得分:0)

正如pappu_kity所说,删除空的catch块会暴露出controller字段为空的事实 - 因此可能尚未自动装配。

要确保使用@Autowired自动装配该字段,您需要通过SpringJUnit4ClassRunner类注释AddSchoolTest来运行测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/path/to/applicationContext.xml" })
public class AddSchoolTest {

    @Autowired
    private UserManagementController controller;

    @Test
    public void testAddSchool() throws Exception {

        ModelMap model = new ModelMap();
        HttpServletRequest request = new MockHttpServletRequest();
        Pricing pricing = new Pricing();
        String userName = "Laily";
        pricing.setName(userName);
        //ModelAndView mav= controller.handleRequest();

        ModelAndView mav= controller.addschool(pricing, model,null, request);
        //fail("Not yet implemented");
        assertNull(pricing);
        assertFalse(model.isEmpty());
        Assert.assertEquals("addschool", mav.getViewName());
    }

}

确保为Spring配置文件的位置正确设置@ContextConfiguration