Uploaded image for project: 'CDAP'
  1. CDAP
  2. CDAP-7391

Unit tests that use org.hamcrest matchers fails with class loading exception

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.5.0
    • Fix Version/s: 4.0.0, 3.6.0, 3.5.2
    • Component/s: Test
    • Labels:
      None
    • Release Notes:
      Fixed TestFramework classloading to support classes that depend on org.hamcrest
    • Rank:
      1|hzzmqn:

      Description

      For example, this test case

        @Test
        public void testFail() {
          Assert.assertThat("oh hello yeah", CoreMatchers.containsString("hello"));
        }
      

      fails with:

      java.lang.LinkageError: loader constraint violation: loader (instance of sun/misc/Launcher$AppClassLoader) previously initiated loading for a different type with name "org/hamcrest/Matcher"
      
      	at java.lang.ClassLoader.defineClass1(Native Method)
      	at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
      	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      	at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
      

      This is due to the way that our TestRunner does class loading: it accepts only classes from org.junit. Junit, however, exposes org.hamcrest in its API, and thus when we load junit, we also load hamcrest. Then, in the test case itself, when the user code uses hamcrest classes, they are loaded with a different class loader and that does not match. The error message from Java is slightly misleading.

        Attachments

          Activity

            People

            • Assignee:
              andreas Andreas Neumann
              Reporter:
              andreas Andreas Neumann
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: