Results 1 to 4 of 4

Thread: Null pointer while testing annotated controller

  1. #1

    Post Null pointer while testing annotated controller

    Hi,

    I am using annotations to create a controller method which accepts a string and returns a ModelAndView object that contains the response object and the view.

    The controller goes something like this.

    Code:
    @Controller
    @RequestMapping("/abc/(*:xyz)")
    public class MyController {
    
       private final Log log = LogFactory.getLog(getClass());
    
       @Autowired(required = true)
       private MyProxy myProxy;
    
       @RequestMapping(method = RequestMethod.GET)
       @SuppressWarnings("unchecked")
       public ModelAndView getMyData(@RequestParam(required = false, value = "xyz") String myStr) {
    
          MyObject myObject= null;
             if (myProxy!= null) {
                     myObject= myProxy.getProxyData(myStr);
                }
          return new ModelAndView("myView", "myData", myObject);
       }
    }

    Now, when I am testing this controller, I am getting a null pointer exception when i call the getMyData() method on the controller. Why isnt my controller object being created ?

    The test class is this :

    Code:
    public class TestMyClass extends TestCase {
    
       private MyController controller;
    
       @Before
       public void setup() {
          controller = new MyController ();
       }
    
       @Test
       public void testMyData()  {
          assertEquals("myView", controller.getMyData("83765876"));
       }
    
    }

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    You are mixing junit styles don't do this either use Junit 3 or Junit4, also why do it in a setup or before method? You don't keep state so simply assign it.

    Code:
    private MyController controller = new MyController();
    
    @Test
    public void testMyData() {
      assertEquals("myView", controller.getMyData("ajf));
    }
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    Thanks Marten .

    I removed the TestCase class inheritance and it works now.

    Code:
    public class TestMyClass {
    
    private MyController controller = new MyController();
    
    @Test
    public void myDataStr() {
      Assert.assertEquals("myView", controller.getMyData("83765876"));
    }
    
    }
    Last edited by Deepti Singh; Jun 13th, 2008 at 03:59 AM.

  4. #4

    Post Testing Autowired Proxy in Controller

    Hi Marten,

    I have another question related to the first.

    My controller has an ejb call . 'MyController' is getting instantiated now, however, its the MyProxy which is not.

    This works fine when called from a browser or a java client, hence am assuming that all the context definitions are working fine. Its only when called from a test class that it doesnt.

    What do i have to do to instantiate the Bean Proxy in my test class ?

    Code:
    @Controller
    @RequestMapping("/abc/(*:xyz)")
    public class MyController {
    
       private final Log log = LogFactory.getLog(getClass());
    
       @Autowired(required = true)
       private MyProxy myProxy;
    
       @RequestMapping(method = RequestMethod.GET)
       @SuppressWarnings("unchecked")
       public ModelAndView getMyData(@RequestParam(required = false, value = "xyz") String myStr) {
    
          MyObject myObject= null;
             if (myProxy!= null) {
                     myObject= myProxy.getProxyData(myStr);
                }
          return new ModelAndView("myView", "myData", myObject);
       }
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •