You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

搭建WireMock Java项目遇NoClassDefFoundError问题求助

Fixing WireMock Java Project NoClassDefFoundError Issue

Let's break down and fix the problem you're facing step by step:

Root Cause of the Error

The java.lang.NoClassDefFoundError: com/google/common/io/Resources happens for two key reasons:

  1. You declared WireMock as a testCompile dependency, which means it's only available to test code—not your main application code. When you run your samplemock main class, the JVM can't find WireMock or its transitive dependencies (like Guava, which provides the missing Resources class).
  2. WireMock 2.17.0 relies on Guava, but since your dependency scope was restricted to tests, this transitive dependency wasn't included in your main runtime classpath.

Step 1: Update Gradle Dependencies

Modify your build.gradle to move WireMock to the main dependency scope, so it's available to your main class. Here's the corrected configuration:

plugins {
    id 'java'
}

repositories {
    mavenCentral() // Ensure you have this to fetch dependencies
}

dependencies {
    // Make WireMock available to main code
    implementation "com.github.tomakehurst:wiremock:2.17.0"
    // Keep JUnit for test code if needed
    testImplementation group: 'junit', name: 'junit', version: '4.12'
}

Step 2: Fix and Enhance Your WireMock Code

First, follow Java naming conventions (capitalize class names) and add a shutdown hook to cleanly stop the server when your app exits. Here's the improved code:

import com.github.tomakehurst.wiremock.WireMockServer;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

public class SampleMock {
    public static void main(String[] args) {
        // Initialize WireMock server on port 9999
        WireMockServer wireMockServer = new WireMockServer(options().port(9999));
        wireMockServer.start();

        // Add shutdown hook to stop server when JVM exits
        Runtime.getRuntime().addShutdownHook(new Thread(wireMockServer::stop));

        // Define your mock endpoint
        stubFor(get(urlEqualTo("/test"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withHeader("Content-Type", "text/plain")
                        .withBody("Hello from WireMock!")));

        System.out.println("WireMock server is running on http://localhost:9999");
        System.out.println("Test the endpoint: curl http://localhost:9999/test");
    }
}

Step 3: Bring Back the __files Folder Functionality

If you want to reuse the __files folder approach (like the standalone jar), configure WireMock to load files from a resources directory:

  1. Create a folder structure: src/main/resources/wiremock/__files
  2. Place your JSON response files (e.g., test-response.json) inside __files
  3. Update your server initialization and stub definition:
// Point WireMock to your resources directory
WireMockServer wireMockServer = new WireMockServer(
    options().port(9999)
            .usingFilesUnderDirectory("src/main/resources/wiremock")
);

// Use the file for response body
stubFor(get(urlEqualTo("/test"))
        .willReturn(aResponse()
                .withStatus(200)
                .withHeader("Content-Type", "application/json")
                .withBodyFile("test-response.json")));

How to Run

  1. Refresh your Gradle project in IntelliJ (click the "Refresh all Gradle projects" button)
  2. Run the SampleMock main class
  3. Test the endpoint with a browser, curl, or Postman: http://localhost:9999/test

内容的提问来源于stack exchange,提问作者user2350138

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:07:58