简体   繁体   English

类的模拟方法

[英]mocking method of class

Exception while running junit 运行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 运行junit给出以下异常

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

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. NoClassDefFoundError是Java运行时系统尝试加载类的定义时抛出的错误,并且该类定义不再可用。 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. 检查记录器配置和您使用过的jar。

meanwhile you can provide the missing dependency for org.apache.logging.log4j.util.ReflectionUtil 同时,您可以为org.apache.logging.log4j.util.ReflectionUtil提供缺少的依赖项

change the log4j version to 2.8.2 see if it helps 将log4j版本更改为2.8.2,查看是否有帮助

//edited version //编辑版本

I have created a project with below configuration to make the logger work 我创建了一个具有以下配置的项目,以使记录器正常工作

build.gradle 的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 使用以下条目在src / main / resources下创建一个log4j2.properties文件,以将日志显示到控制台

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) 以下是调用记录器的Java代码(为进行测试,我将其放在了main方法中)

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM