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

使用Mockito时触发java.lang.NoClassDefFoundError错误求解决

Troubleshooting Mockito NoClassDefFoundError Issue

Hey there, let's work through this Mockito problem step by step! From your description, you're hitting a common issue where the JVM can't find Mockito's classes at runtime. Let's break down the possible causes and fixes:

1. Dependency Scope & Execution Method Mismatch

Looking at your stack trace, you're running your code via a main method, but your Mockito dependency has <scope>test</scope>. Maven's test scope means the dependency is only available for test sources (like classes under src/test/java) and won't be included in the runtime classpath for main code execution.

Fix:

  • Instead of running via a main method, refactor your code into a proper JUnit test class. For example:
    import org.junit.Before;
    import org.junit.Test;
    import org.mockito.Mockito;
    
    public class PortfolioTester {
        private Portfolio portfolio;
        private StockService mockStockService;
    
        @Before
        public void setUp() {
            mockStockService = Mockito.mock(StockService.class);
            portfolio = new Portfolio(mockStockService);
        }
    
        @Test
        public void testPortfolioValue() {
            // Your test logic here
        }
    }
    
  • Then run this class using your IDE's JUnit runner (right-click the class -> Run as JUnit Test), which will automatically include test-scoped dependencies.

If you absolutely need to use a main method for testing (not recommended for proper unit tests), you can temporarily change the dependency scope to compile (but remember to switch it back to test once you're done, as Mockito is meant for testing only).

2. Outdated & Deprecated Dependency

You're using mockito-all:2.0.2-beta, which is an old, beta version of the deprecated aggregate JAR. Mockito has phased out mockito-all in favor of mockito-core since version 2.x, and beta versions can have compatibility or stability issues.

Fix:
Replace your existing Mockito dependency with the latest stable mockito-core version (check for the latest version on Maven Central, but here's a recent example):

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>5.6.0</version>
    <scope>test</scope>
</dependency>

3. Ensure Dependencies Are Properly Downloaded

Sometimes Maven doesn't download dependencies correctly. Force a refresh to make sure the Mockito JAR is in your local repository:

  • In your IDE: Right-click your project -> Maven -> Reimport
  • Via command line: Run mvn clean install -U in your project directory (the -U flag forces Maven to update snapshots/releases)

4. Verify JDK Compatibility

Make sure your JDK version is compatible with the Mockito version you're using. For example:

  • Mockito 5.x requires JDK 11+
  • Mockito 4.x works with JDK 8+

Double-check that your JAVA_HOME environment variable points to the correct JDK version, and that your IDE is using this JDK for the project.

After trying these steps, your Mockito tests should run without the class not found error!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:30:27