搭建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:
- You declared WireMock as a
testCompiledependency, which means it's only available to test code—not your main application code. When you run yoursamplemockmain class, the JVM can't find WireMock or its transitive dependencies (like Guava, which provides the missingResourcesclass). - 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:
- Create a folder structure:
src/main/resources/wiremock/__files - Place your JSON response files (e.g.,
test-response.json) inside__files - 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
- Refresh your Gradle project in IntelliJ (click the "Refresh all Gradle projects" button)
- Run the
SampleMockmain class - Test the endpoint with a browser, curl, or Postman:
http://localhost:9999/test
内容的提问来源于stack exchange,提问作者user2350138
相关产品推荐
相关产品推荐

