Uploaded image for project: 'Red Hat Process Automation Manager'
  1. Red Hat Process Automation Manager
  2. RHPAM-2174

JUnit tests in src/test/java folder unable to find classes in src/main/java

XMLWordPrintable

    • Hide
      1. Create new project on Business Central (I named mine "junit-fails")
      2. Add a new "Data Object" asset. (I named mine MyClass).

      3. Git clone the project from Guvnor to a different local folder.
      4. Import local cloned project to any IDE (I used RedHat Code Ready).
      5. Create a JUnit test class within src/test/java in the same Java package, with a test method that uses the "Data Object" meaningfully in some manner. (I instantiated MyClass and performed a simple assertEquals() on a property).
      6. Commit the JUnit class.
      7. Reimport the project back to Business Central, so that the JUnit class shows.
      8. Attempt to build/deploy through Business Central using "Build" or "Deploy" buttons.

      Show
      Create new project on Business Central (I named mine "junit-fails") Add a new "Data Object" asset. (I named mine MyClass ). Git clone the project from Guvnor to a different local folder. Import local cloned project to any IDE (I used RedHat Code Ready). Create a JUnit test class within src/test/java in the same Java package, with a test method that uses the "Data Object" meaningfully in some manner. (I instantiated MyClass and performed a simple assertEquals() on a property). Commit the JUnit class. Reimport the project back to Business Central, so that the JUnit class shows. Attempt to build/deploy through Business Central using "Build" or "Deploy" buttons.

      A JUnit class created outside of Business Central within a Maven src/test/java folder location is unable to pick up classes within the same package found within the Maven src/main/java folder location.

      As a separate issue, JUnit classes are recognized as "Data Object" assets by Business Central, seemingly by virtue of simply being a .java file. It throws an error as well. That should be an issue for another task.

      A workaround exist to make Business Central happy, which I will describe here... But it's not a feasible alternative to actual work

      The workaround is to place the JUnit classes within the Maven src/main/java folder location instead of the conventional src/test/java folder location.

      This workaround works, but it is not ideal for several reasons:

      1. Test resources should not be packaged into the JAR artifact.
      2. Maven dependencies in test scope are technically not in the classpath of src/main/java classes, but Business Central is happy when clicking on the "Build" and "Deploy" buttons.
      3. The two reasons above breaks Maven builds in CI/CD environments (i.e. Jenkins) because it will not successfully compile a Maven build when JUnit classes and annotations are used without JUnit Maven dependencies in the classpath. Of course, you can modify the POM to place JUnit in compile scope, but that violates best practices.

        1. Build-fail-for-MyClassTest.png
          Build-fail-for-MyClassTest.png
          50 kB
        2. JUnit-test-class-open-error.png
          JUnit-test-class-open-error.png
          27 kB
        3. MyClass.java.png
          MyClass.java.png
          37 kB
        4. MyClass.png
          MyClass.png
          39 kB
        5. MyClassTest.java.png
          MyClassTest.java.png
          46 kB
        6. MyClassTest.png
          MyClassTest.png
          37 kB

              eignatow Eder Ignatowicz
              andrewlee1982 Andrew Lee (Inactive)
              Jan Stastny Jan Stastny
              Jan Stastny Jan Stastny
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: