Question

I am new to writing test cases:

My controller is below :

 @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;
           }

and my junit test case is this :

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

        }
    }

}

but my problem is even if I will remove "add school" and put something else it wil show green but if I remove try and catch than in every case it wil give red. What is the problem.

Full stack trace is this

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
Was it helpful?

Solution

As pappu_kity says, removing the empty catch block exposes the fact that your controller field is null - and so probably hasn't been autowired.

To ensure that the field is autowired using @Autowired you need to run the test with the SpringJUnit4ClassRunner by annotating the AddSchoolTest class:

@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());
    }

}

Ensure that the @ContextConfiguration is set appropriately for the locations of your Spring config files.

OTHER TIPS

you can try changing

ModelAndView mv = new ModelAndView(ADDSCHOOL);

to ModelAndView mv = new ModelAndView(addschool); or

ModelAndView mv = new ModelAndView(pricing);

as it is in the @ModelAttribute("addschool") Pricing pricing in your controller action .

I am not sure but just can see whether it works or not

Thanks.

if it is red then your test case is falling, your catch block is empty, print the stack trace and see what is the problem , upon exception your test case will fail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top