stefanbirkner / system-rules

A collection of JUnit rules for testing code which uses java.lang.System.
http://stefanbirkner.github.io/system-rules
Other
546 stars 71 forks source link

SystemOutRule initializationError when running from Linux command line #80

Closed PreciousChicken closed 4 years ago

PreciousChicken commented 4 years ago

I have downloaded files junit-platform-console-standalone-1.6.0.jar and system-rules-1.19.0.jar to my working directory.

Within this directory I create a file MyTest.java with the following content:

import static org.junit.Assert.assertEquals;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.Test;
import org.junit.Rule;

public class MyTest {

    @Rule
    public SystemOutRule systemOutRule = new SystemOutRule().enableLog();

    @Test
    public void writesTextToSystemOut() {
        System.out.print("hello world");
        assertEquals("hello world", systemOutRule.getLog());
    }
}

I compile the file without errors with the following command:

javac -cp .:junit-platform-console-standalone-1.6.0.jar:system-rules-1.19.0.jar MyTest.java

I run the file with: java -jar junit-platform-console-standalone-1.6.0.jar --class-path . -c MyTest

and get the following error:

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

╷
├─ JUnit Jupiter ✔
└─ JUnit Vintage ✔
   └─ MyTest ✔
      └─ initializationError ✘ Lorg/junit/contrib/java/lang/system/SystemOutRule;

Failures (1):
  JUnit Vintage:MyTest:initializationError
    => java.lang.NoClassDefFoundError: Lorg/junit/contrib/java/lang/system/SystemOutRule;
       java.base/java.lang.Class.getDeclaredFields0(Native Method)
       java.base/java.lang.Class.privateGetDeclaredFields(Class.java:3061)
       java.base/java.lang.Class.getDeclaredFields(Class.java:2248)
       org.junit.runners.model.TestClass.getSortedDeclaredFields(TestClass.java:77)
       org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:70)
       [...]
     Caused by: java.lang.ClassNotFoundException: org.junit.contrib.java.lang.system.SystemOutRule
       java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:471)
       java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
       java.base/java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:899)
       java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
       [...]

Test run finished after 40 ms
[         3 containers found      ]
[         0 containers skipped    ]
[         3 containers started    ]
[         0 containers aborted    ]
[         3 containers successful ]
[         0 containers failed     ]
[         1 tests found           ]
[         0 tests skipped         ]
[         1 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         1 tests failed          ]

Is this a faulty configuration on my part (probably), or an issue with system-rules?

Appreciate any help.

NB - My OS is Ubuntu 18.04.4 LTS and I'm using OpenJDK version 11.0.6.

PreciousChicken commented 4 years ago

Sorry this was indeed my fault, you need to run the file as:

java -jar junit-platform-console-standalone-1.6.0.jar --class-path .:system-rules-1.19.0.jar -c MyTest

which produces:

hello world
Thanks for using JUnit! Support its development at https://junit.org/sponsoring

╷
├─ JUnit Jupiter ✔
└─ JUnit Vintage ✔
   └─ MyTest ✔
      └─ writesTextToSystemOut ✔

Test run finished after 46 ms
[         3 containers found      ]
[         0 containers skipped    ]
[         3 containers started    ]
[         0 containers aborted    ]
[         3 containers successful ]
[         0 containers failed     ]
[         1 tests found           ]
[         0 tests skipped         ]
[         1 tests started         ]
[         0 tests aborted         ]
[         1 tests successful      ]
[         0 tests failed          ]

Every day is a learning day!

NB - As an aide-memoire I've also put this solution in blog form at System Rules on the Command Line.