0

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

3 Answers3

0

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.

Rashedul.Rubel
  • 3,446
  • 25
  • 36
  • final static String ADDSCHOOL = "addschool"; already this statement is there in the controller. – user2893700 Oct 23 '13 at 07:13
  • change ModelAndView mav= controller.addschool(pricing, model,null, request); to ModelAndView mav= controller.addschool(pricing, model,pricing, request); inside your try block to avoid null pointer exception – Rashedul.Rubel Oct 23 '13 at 10:37
0

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.

pappu_kutty
  • 2,378
  • 8
  • 49
  • 93
  • It is giving ths on printing stack trace – user2893700 Oct 23 '13 at 09:17
  • It is giving ths on printing stack trace [Ljava.lang.StackTraceElement;@406199 I think no exception is thrown but if I remove try and catch block then I am getting red and null pointer exception is thrown at ModelAndView mav= controller.addlibrary(pricing, model,null, request); ths statement . hw to remove ths exception. – user2893700 Oct 23 '13 at 09:23
  • yes you have empty catch block , so you are swallowing exception in catch block. the cause for your exception will be your controller is not wired at all, hence you are getting nullpointer. i suspect that you might miss adding spring junit runner. check this link http://www.springbyexample.org/examples/simple-spring-transactional-junit4-test.html – pappu_kutty Oct 23 '13 at 09:40
0

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.

Will Keeling
  • 22,055
  • 4
  • 51
  • 61
  • Thanks Will but I am now getting this error Could not autowire field: private com.enbee.admin.controller.AddInstiDeptLibController – user2893700 Oct 24 '13 at 09:45
  • Because it's running as an integration test it will attempt to bring up the whole Spring application context and do all the wiring. Does your `@ContextConfiguration` specify the correct locations to your Spring config flles? Could you post the full stacktrace? – Will Keeling Oct 24 '13 at 09:50
  • Will, I post the full stack trace but Do I need to change something in spring-servlet also? – user2893700 Oct 24 '13 at 10:43
  • It looks as though Spring can't find `AddInstiDeptLibController` to autowire into your `AddSchoolTest`. The code in your original post is testing a `UserManagementController`. Have you changed this to a `AddInstiDeptLibController`? – Will Keeling Oct 24 '13 at 11:11