Uploaded image for project: 'Maven Surefire'
  1. Maven Surefire
  2. SUREFIRE-114

Surefire plugin throws NoSuchMethodException when errors occur during TestSetup decorator

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0 (2.2 plugin)
    • 2.3
    • None
    • None

    Description

      I've included an example maven project, "setup-bug.zip". It was created by doing "mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=setup-bug" and then modifying AppTest.java to use a "TestSetup" decorator.

      Normally JUnit runs your setUp()/tearDown() methods once for every test method... so if you've got a TestCase with methods testFoo and testBar, JUnit will normally run like this: setUp testFoo tearDown setUp testBar tearDown. You can use a TestSetup decorator to run your setUp function just once for the class: TestSetup.setUp testFoo testBar TestSetup.tearDown.

      In the example given, the test's setup method throws a RuntimeException (as tests sometimes do). Instead of handling this gracefully, Maven Surefire throws a NoSuchMethodException as it attempts to call .getName() on the TestSetup object (which isn't guaranteed to have a name).

      Running com.mycompany.app.AppTest
      org.apache.maven.surefire.booter.SurefireExecutionException: com.mycompany.app.AppTest; nested exception is java.lang.reflect.UndeclaredThrowableException: null; nested exception is org.apache.maven.surefire.testset.TestSetFailedException: com.mycompany.app.AppTest; nested exception is java.lang.reflect.UndeclaredThrowableException: null
      org.apache.maven.surefire.testset.TestSetFailedException: com.mycompany.app.AppTest; nested exception is java.lang.reflect.UndeclaredThrowableException: null
      java.lang.reflect.UndeclaredThrowableException
      at $Proxy0.addError(Unknown Source)
      at junit.framework.TestResult.addError(TestResult.java:36)
      at junit.framework.TestResult.runProtected(TestResult.java:133)
      at junit.extensions.TestSetup.run(TestSetup.java:23)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:210)
      at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:135)
      at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:122)
      at org.apache.maven.surefire.Surefire.run(Surefire.java:129)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:324)
      at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:225)
      at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:747)
      Caused by: java.lang.NoSuchMethodException: com.mycompany.app.AppTest$AppTestSetup.getName()
      at java.lang.Class.getMethod(Class.java:986)
      at org.apache.maven.surefire.junit.TestListenerInvocationHandler.getStackTraceWriter(TestListenerInvocationHandler.java:171)
      at org.apache.maven.surefire.junit.TestListenerInvocationHandler.handleAddError(TestListenerInvocationHandler.java:160)
      at org.apache.maven.surefire.junit.TestListenerInvocationHandler.invoke(TestListenerInvocationHandler.java:134)
      ... 18 more
      [INFO] ------------------------------------------------------------------------
      [ERROR] BUILD FAILURE
      [INFO] ------------------------------------------------------------------------

      You can workaround this by hardcoding a public String getName() method on the TestSetup object and ensuring that it's public.

      Attachments

        1. setup-bug.zip
          4 kB
          Daniel Fabulich

        Activity

          People

            kenneyw kenneyw
            dfabulich Daniel Fabulich
            Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: