2013-10-23 86 views
0

我是新来编写测试用例:JUnit测试案例给错误

我控制器低于:

@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 

回答

0

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

为了确保场自动装配使用@Autowired你需要通过注释的AddSchoolTest类运行与SpringJUnit4ClassRunner测试:

@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配置文件的位置。

+0

谢谢你会但现在我得到这个错误无法autowire字段:私人com.enbee.admin.controller。AddInstiDeptLibController – user2893700

+0

因为它是作为集成测试运行的,所以它会尝试调用整个Spring应用程序上下文并进行所有连线。你的'@ ContextConfiguration'是否为你的Spring配置文件指定了正确的位置?你能发布完整的stacktrace吗? –

+0

请问,我发布了完整的堆栈跟踪,但我是否还需要在spring-servlet中更改某些内容? – user2893700

0

你可以尝试改变

ModelAndView mv = new ModelAndView(ADDSCHOOL); 

ModelAndView mv = new ModelAndView(addschool);

ModelAndView mv = new ModelAndView(pricing); 

,因为它是在你的控制器动作@ModelAttribute("addschool") Pricing pricing

我不知道,但只可以看到它是否有效与否

感谢。

+0

final static String ADDSCHOOL =“addschool”;这个声明已经在控制器里了。 – user2893700

+0

你可以建议别的什么.. – user2893700

+0

什么是错误信息? –

0

如果它是红色的,那么你的测试用例正在下降,你的catch块是空的,打印堆栈跟踪,看看有什么问题,例外情况是你的测试用例会失败。

+0

这是给ths打印堆栈跟踪 – user2893700

+0

这是给ths打印堆栈跟踪[Ljava.lang.StackTraceElement; @ 406199我认为没有例外抛出,但如果我删除try和catch块那么我会变得红色,并在ModelAndView引发空指针异常mav = controller.addlibrary(pricing,model,null,request);声明。 hw删除这个异常。 – user2893700

+0

是的,你有空的catch块,所以你在catch块中吞下异常。你的异常的原因将是你的控制器根本没有连线,因此你得到了空指针。我怀疑你可能会错过加入spring junit亚军。检查此链接http://www.springbyexample.org/examples/simple-spring-transactional-junit4-test.html –