类的模拟方法

运行junit时出现异常


class JSONTool {

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


    public Object fetch(String url) {

        return invokeEndPoint(url, 2000, new HashMap<String, String>());

    }

}

我想测试这个类,下面是测试方法


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!");

}

运行junit给出以下异常


java.lang.NoClassDefFoundError:

  org/apache/logging/log4j/util/ReflectionUtil

  Caused by: java.lang.ClassNotFoundException:

  org.apache.logging.log4j.util.ReflectionUtil


汪汪一只猫
浏览 102回答 1
1回答

动漫人物

NoClassDefFoundError是当 Java 运行时系统尝试加载类的定义并且该类定义不再可用时引发的错误。所需的类定义在编译时存在,但在运行时丢失。检查记录器配置和您使用的 jar。同时,您可以为 org.apache.logging.log4j.util.ReflectionUtil 提供缺少的依赖项将 log4j 版本更改为 2.8.2 看看是否有帮助//修改后的版本我创建了一个具有以下配置的项目以使记录器工作构建.gradle/*&nbsp;* This file was generated by the Gradle 'init' task.&nbsp;*&nbsp;* This generated file contains a sample Java Library project to get you started.&nbsp;* For more details take a look at the Java Libraries chapter in the Gradle&nbsp;* user guide available at https://docs.gradle.org/5.0/userguide/java_library_plugin.html&nbsp;*/plugins {&nbsp; &nbsp; // Apply the java-library plugin to add support for Java Library&nbsp; &nbsp; id 'java-library'}repositories {&nbsp; &nbsp; // Use jcenter for resolving your dependencies.&nbsp; &nbsp; // You can declare any Maven/Ivy/file repository here.&nbsp; &nbsp; jcenter()}dependencies {&nbsp; &nbsp; // This dependency is exported to consumers, that is to say found on their compile classpath.&nbsp; &nbsp; api 'org.apache.commons:commons-math3:3.6.1'&nbsp; &nbsp; api 'org.apache.logging.log4j:log4j-web:2.8.2'&nbsp; &nbsp; // This dependency is used internally, and not exposed to consumers on their own compile classpath.&nbsp; &nbsp; implementation 'com.google.guava:guava:26.0-jre'&nbsp; &nbsp; // Use JUnit test framework&nbsp; &nbsp; testImplementation 'junit:junit:4.12'}在src/main/resources下创建一个log4j2.properties文件,其中包含以下条目以将日志显示到控制台appender.console.type = Consoleappender.console.name = STDOUTrootLogger.level = debugrootLogger.appenderRefs = stdoutrootLogger.appenderRef.stdout.ref = STDOUT下面是调用记录器的 Java 代码(为了测试,我把它放在一个 main 方法中)package GradleEclipseTestProject;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;public class GradleLog4jTest {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Testing log4j dependencies");&nbsp; &nbsp; &nbsp; &nbsp; Logger logger = LogManager.getLogger();&nbsp; &nbsp; &nbsp; &nbsp; logger.info("This is a sample logger");&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java