使用Mockito MockedConstruction会引发NoClassDefFoundError

kpbwa7wx  于 2023-02-08  发布在  其他
关注(0)|答案(1)|浏览(476)

我有一个用于获取URL的简单类:

public class UrlFetcher {

    private URL url;

    public UrlFetcher(String address) throws IOException {
        url = new URL(address);
    }

    public int getResponseCode() throws IOException {
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        return con.getResponseCode();
    }

}

我正在试验Mockito内联模拟器和MockedConstruction类:

@Test
    public void givenMockedUrl_whenRequestSent_thenHttpOKReceivedSuccessfully() throws IOException {
        try (MockedConstruction<URL> mocked = Mockito.mockConstruction(URL.class, (mock, context) -> {
            
            HttpURLConnection mockConnection = Mockito.mock(HttpURLConnection.class);
            Mockito.when(mockConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_REQUEST);

            Mockito.when(mock.openConnection()).thenReturn(mockConnection);
            
            
        })) {
            UrlFetcher fetcher = new UrlFetcher("https://www.google.com/");
            assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, fetcher.getResponseCode());
        }
        
    }

我试图在构造函数中模拟url,但是当我运行测试时,它说:

java.lang.NoClassDefFoundError: com/mypackage/mock/url/UrlFetcher
    at com.mypackage.mock.url.UrlFetcherMockitoUnitTest.givenMockedUrl_whenRequestSent_thenHttpOKReceivedSuccessfully(UrlFetcherMockitoUnitTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:72)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
    at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:768)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: java.lang.ClassNotFoundException: com.mypackage.mock.url.UrlFetcher
    at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 41 more

我真的不明白这个错误。如果我删除MockedConstruction,NoClassDefFounderror错误就会消失。
我使用的是Mockito核心4.4.0和Mockito倾斜4.4.0
谢谢

yqkkidmi

yqkkidmi1#

我在过去的几天里遇到了同样的问题,不幸的是,我没有找到任何解决这个问题的方法。在我看来,这是新版本Mockito的一个bug。
我对这个问题非常恼火,所以我的建议是改变方法,不要模拟URL.class,而是注入一个本地文件URL(如<your_repo>file:///C:/Users//src/test/resources/_temp/file.txt)作为输入字符串参数(在本例中为“address”)。
这样做,您就不需要模拟URL.class的构造函数,而且您的测试也不会进行任何外部连接,从而有效地保持了单元测试的状态。

相关问题