亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

類的模擬方法

類的模擬方法

汪汪一只貓 2022-06-15 14:41:24
運行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
查看完整描述

1 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

NoClassDefFoundError是當 Java 運行時系統嘗試加載類的定義并且該類定義不再可用時引發的錯誤。所需的類定義在編譯時存在,但在運行時丟失。


檢查記錄器配置和您使用的 jar。


同時,您可以為 org.apache.logging.log4j.util.ReflectionUtil 提供缺少的依賴項


將 log4j 版本更改為 2.8.2 看看是否有幫助


//修改后的版本


我創建了一個具有以下配置的項目以使記錄器工作


構建.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'

}

在src/main/resources下創建一個log4j2.properties文件,其中包含以下條目以將日志顯示到控制臺


appender.console.type = Console

appender.console.name = STDOUT



rootLogger.level = debug

rootLogger.appenderRefs = stdout

rootLogger.appenderRef.stdout.ref = STDOUT

下面是調用記錄器的 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");

    }


}


查看完整回答
反對 回復 2022-06-15
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號