Announcement Announcement Module
Collapse
No announcement yet.
Null pointer while testing annotated controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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));
    }

    Comment


    • #3
      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, 04:59 AM.

      Comment


      • #4
        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);
           }
        }

        Comment

        Working...
        X