0

Exception while running junit

class JSONTool {
    private static MipLogger logger = MipLogger.getLogger(MIPJsonTool.class);

    public Object fetch(String url) {
        return invokeEndPoint(url, 2000, new HashMap<String, String>());
    }
}

i want to test this class and below is the test method

public void testFetchString() {
    JSONTool mipJsonTool = new JSONTool();
    JSONTool mipJsonTool1 = Mockito.spy(mipJsonTool);       
    Mockito.doReturn(new JSONObject()
            .put("status", 200))
            .when(mipJsonTool1)
            .fetch("http://localhost:5000/author");
    Object obj = mipJsonTool1.fetch("http://localhost:5000/author");
    System.out.println("Done!");
}

running junit gives below exception

java.lang.NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.util.ReflectionUtil

Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37
  • Do you have log4j at your classpath? – TobiSH Feb 12 '19 at 07:24
  • https://stackoverflow.com/questions/52700803/im-getting-noclassdeffounderror-org-apache-logging-log4j-util-reflectionutil – PolishCivil Feb 12 '19 at 07:24
  • @TobiSH i have log4j in classpath – Naresh Kumar Feb 12 '19 at 07:25
  • But wrong version – PolishCivil Feb 12 '19 at 07:27
  • 2
    Possible duplicate of [I'm getting "NoClassDefFoundError: org/apache/logging/log4j/util/ReflectionUtil"](https://stackoverflow.com/questions/52700803/im-getting-noclassdeffounderror-org-apache-logging-log4j-util-reflectionutil) – TobiSH Feb 12 '19 at 07:27
  • As @PolishCivil already mentioned. You might have the wrong version. Can you show us what you have on your classpath (incl. versions). I guess your problem has actually nothing to do with mocking a class. – TobiSH Feb 12 '19 at 07:29
  • compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.8.2' compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.8.2' compile group: 'org.apache.logging.log4j', name: 'log4j-1.2-api', version: '2.8.2' compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: '2.8.2' compile group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.8.2' – Naresh Kumar Feb 12 '19 at 08:55

1 Answers1

0

NoClassDefFoundError is an error that is thrown when the Java Runtime System tries to load the definition of a class, and that class definition is no longer available. The required class definition was present at compile time, but it was missing at runtime.

Check the logger configuration and the jar that you have used.

meanwhile you can provide the missing dependency for org.apache.logging.log4j.util.ReflectionUtil

change the log4j version to 2.8.2 see if it helps

//edited version

I have created a project with below configuration to make the logger work

build.gradle

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * user guide available at https://docs.gradle.org/5.0/userguide/java_library_plugin.html
 */

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
}

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'
    api 'org.apache.logging.log4j:log4j-web:2.8.2'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:26.0-jre'


    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

Create a log4j2.properties file under src/main/resources with below entries to show logs to console

appender.console.type = Console
appender.console.name = STDOUT


rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT

Below is the Java code that Invoke the logger (for testing I have put it inside a main method)

package GradleEclipseTestProject;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class GradleLog4jTest {


    public static void main(String[] args) {
        System.out.println("Testing log4j dependencies");
        Logger logger = LogManager.getLogger();
        logger.info("This is a sample logger");
    }

}
Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37
  • I have below dependecies for log4j compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.8.2' compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.8.2' compile group: 'org.apache.logging.log4j', name: 'log4j-1.2-api', version: '2.8.2' compile group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: '2.8.2' compile group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.8.2' – Naresh Kumar Feb 12 '19 at 08:35
  • Are by any chance you are using SprinBoot ? – Amit Kumar Lal Feb 12 '19 at 08:42
  • i am working on java gradle project not spring one, will it work different for spring project – Naresh Kumar Feb 12 '19 at 08:52
  • @NareshKumar you can remove the other test and google guava dependencies it will work, that was the default addition. – Amit Kumar Lal Feb 13 '19 at 07:32
  • @NareshKumar do you mind either accepting the answer or let me know if that's not what u wanted so that i can help you out with the problem ? – Amit Kumar Lal Feb 14 '19 at 16:25
  • 1
    Thanks @hades for helping me on this. – Naresh Kumar Feb 16 '19 at 12:44
  • you are welcome .. meanwhile do you mind upvoting the answer also as it will help me eating reputation. thanks – Amit Kumar Lal Feb 16 '19 at 13:53