1

This must be obvious, but I'm stuck after looking here and elsewhere. I want to run my Junit test from the command line. I do a scripted deployment process and I want to verify before doing the deployment.

I found this SO but I'm getting an error (more below). How to run JUnit test cases from the command line

I can run this: java -cp .:/path/junit-4.8.1.jar org.junit.runner.JUnitCore HappyPath.class

and I am in this directory with the .class file.

$ ls -lart
total 40
-rw-r--r--  1 rdejournett  staff    144 Sep 22 12:09 package-info.class
-rw-r--r--  1 rdejournett  staff  11001 Sep 22 12:09 HappyPath.class
drwxr-xr-x  5 rdejournett  staff    170 Sep 22 12:09 ..
-rw-r--r--  1 rdejournett  staff    660 Sep 22 12:13 AllTests.class
drwxr-xr-x  5 rdejournett  staff    170 Sep 22 12:13 .

But the output is:

JUnit version 4.8.1
Could not find class: HappyPath.class

Time: 0

OK (0 tests)

Do I need to create a JAR file?

The HappyPath class looks like this.

@Test
    public static void happyPath() {

        String xml = "";
        xml = ReadJson.ReadFile("/app/mirth/UnitTests/GEoutput.xml");
        Statements s = new Statements();
        // need XmlDocRoot tag or whatever to parse it properly

        try {
            s = ConvertXmltoObj(xml);
            happyPathStatement(s);
            happyPathGuarantor(s);
            happyPathAging(s);
            happyPathEncounters(s);
            happyPathEncounterCharges(s);
        } catch (JAXBException e) {
            e.printStackTrace();
            fail();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            fail();
        } catch (SAXException e) {
            e.printStackTrace();
            fail();
        } catch (IOException e) {
            e.printStackTrace();
            fail();
        } 
        assertTrue(true);
    }

Update:

Removed the .class, so command is this:

java -cp .:/path/junit-4.8.1.jar org.junit.runner.JUnitCore HappyPath

Getting this error now:

JUnit version 4.8.1
Exception in thread "main" java.lang.NoClassDefFoundError: HappyPath (wrong name: com/xxx/xxx/datamodel/ge/HappyPath)
    at java.lang.ClassLoader.defineClass1(Native Method)
Community
  • 1
  • 1
Rob
  • 2,363
  • 7
  • 36
  • 54
  • 1
    use something like maven or gradle to build your project. It comes with junit support so you can easily run your tests. – Jilles van Gurp Sep 22 '15 at 16:29
  • Thanks, getting this now: JUnit version 4.8.1 Exception in thread "main" java.lang.NoClassDefFoundError: HappyPath (wrong name: com/xxx/xxx/datamodel/ge/HappyPath) at java.lang.ClassLoader.defineClass1(Native Method) – Rob Sep 22 '15 at 16:45

1 Answers1

2

I ended up finding a solution after a bunch more research.

junit: could not find test class

This SO helped greatly in figuring out root cause.

The command I ended up running was:

java -cp .:/app/mirth/UnitTests/junit-4.8.1.jar:/app/mirth/UnitTests/hamcrest-core-1.3.jar:/Applications/Mirth\ Connect/custom-lib/estatement-obj.jar com.xxx.xxx.datamodel.ge.HappyPath

from this directory:

/Users/me/mfss-rcm/mfss-rcm-mirth/Myproject/target/test-classes

Basically you need to start from the root and use the FQN, which java interprets as a relative path. So that the class file is actually here:

/Users/me/mfss-rcm/mfss-rcm-mirth/Myproject/target/test-classes/com/xxx/xxx/datamodel/ge

I needed to add the junit jar, a dependancy jar, and my actual code to test as a jar (not sure how to get around the requirement to package it as a jar, anyway it's not a big deal.).

ALso I added a main method to the test class which calls the test class.

public static void main(String[] args) {
    System.err.println("Starting Happy Path Testing");
    happyPath();
}
Community
  • 1
  • 1
Rob
  • 2,363
  • 7
  • 36
  • 54