Reputation: 1
I copied a unit test that was in a book. exactly the same. There is no problem in compiler but when it runs there are some exceptions. Is there any problem in my codes? if no so what is the reason and what is the solution?
This is my class:
package Testing;
public class MessageUtil {
private String message;
public MessageUtil(String message) {
this.message = message;
}
public String printMessage() {
System.out.println(message);
return message;
}
public static void main(String[] args) {
}
}
This is test case:
package Testing;
import org.junit.Test;
import junit.framework.TestCase;
public class TestJunit extends TestCase {
String message = "Hello World";
MessageUtil messageUtil = new MessageUtil(message);
@Test
public void testPrintMessage() {
assertEquals(message,messageUtil.printMessage());
}
}
This is test runner:
package Testing;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
This is printed exceptions:
Exception in thread "main" java.lang.NoClassDefFoundError: junit/framework/TestCase
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:825)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:723)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:646)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at Testing.TestRunner.main(TestRunner.java:11)
Caused by: java.lang.ClassNotFoundException: junit.framework.TestCase
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 10 more
Upvotes: 0
Views: 717
Reputation: 311
It looks like you're using an outdated book to learn JUnit. My initial suggestion would be to buy an updated one, covering the latest version of JUnit, or look for documentation directly on the JUnit website: https://junit.org/junit5/
Answering your question, the java.lang.ClassNotFoundException is thrown when the Java Virtual Machine (JVM) tries to load a class and this specified class cannot be found in the classpath. In your case, the type junit.framework.TestCase was not found by the JVM, so it wasn't able to compile/run your Java program.
You can resolve this issue by adding JUnit Jar to your classpath. Take a look at this page to find out how to do that: https://examples.javacodegeeks.com/how-to-set-classpath-in-java/
Ps: You didn't mention which IDE you are using, so I suggest you start by taking a look into VisualStudioCode with the Java Extension Pack installed, as suggested here: https://junit.org/junit5/docs/current/user-guide/#running-tests-ide-vscode.
Upvotes: 1