0

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

我的控制器如下:

 @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){

        }
    }

}

但是我的问题是,即使我将删除“添加学校”并放置其他内容,它也会显示为绿色,但是如果我删除 try and catch ,那么在每种情况下它都会显示为红色。问题是什么。

完整的堆栈跟踪是这个

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
4

3 回答 3

0

你可以尝试改变

ModelAndView mv = new ModelAndView(ADDSCHOOL);

ModelAndView mv = new ModelAndView(addschool);

ModelAndView mv = new ModelAndView(pricing);

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

我不确定,但可以看看它是否有效

谢谢。

于 2013-10-23T07:03:54.167 回答
0

如果它是红色的,那么您的测试用例正在下降,您的 catch 块是空的,打印堆栈跟踪并查看问题所在,一旦出现异常,您的测试用例将失败。

于 2013-10-23T08:42:09.977 回答
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());
    }

}

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

于 2013-10-23T13:36:35.010 回答