Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve a NoSuchMethodError in a Spring Controller testcase?

java.lang.NoSuchMethodError: 
org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/ 
runner/Description;Ljava/lang/Throwable;)
        at  org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestM­ethod(SpringJUnit4ClassRunner.java:
155) 

And written testcase for controller like, newly writing testcases for Spring Controller classes:

TestXController.java

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"file:D:/ABC/src/main/webapp/WEB-INF/
xyz-servlet.xml", 
"file:D:/ABC/src/main/webapp/WEB-INF/xyzrest-servlet.xml"}) 

    public class TestXController  { 

    @Inject 
    private ApplicationContext applicationContext; 

    private MockHttpServletRequest request; 
    private MockHttpServletResponse response; 
    private HandlerAdapter handlerAdapter; 
    private XController controller; 

    @Test 
    public void setUp() { 
       request = new MockHttpServletRequest(); 
       response = new MockHttpServletResponse(); 
       handlerAdapter =  applicationContext.getBean(HandlerAdapter.class); 
       // I could get the controller from the context here 
       controller = new XController(); 
    } 

    @Test 
    public void testgoLoginPage() throws Exception { 

        request.setAttribute("login", "0"); 

        final org.springframework.web.servlet.ModelAndView mav =  handlerAdapter.handle(request, response, controller); 
        assertViewName(mav, null); 
        assertAndReturnModelAttributeOfType(mav, "login", null); 
    } 

    @Test 
    public void testgoHomePage(){ 

        org.springframework.web.servlet.ModelAndView mav =null; 

        request.setAttribute("success1", "1"); 
        request.setAttribute("success", "1"); 

                try { 
                        mav = handlerAdapter.handle(request, response, controller); 
                } catch (Exception e) { 
                        // TODO Auto-generated catch block 
                        e.printStackTrace(); 
                } 

        assertViewName(mav, null); 
        assertAndReturnModelAttributeOfType(mav, "home",null); 
    } 

Can any one Guide me on this to write test cases for Spring Controller classes,Or any code samples links.

Thanks & Regards, Venu Gopala Reddy.

like image 801
user1403150 Avatar asked May 18 '12 11:05

user1403150


1 Answers

Yes, make sure you're using the right version of JUnit. I think there's a mismatch with the Spring test JAR that forces you to use JUnit 4.4.

like image 173
duffymo Avatar answered Oct 19 '22 06:10

duffymo