Тип макета должен быть: URL, но это: URL $$ PowerMock0 - PullRequest
0 голосов
/ 15 апреля 2020

Может кто-нибудь определить, почему я получаю это исключение, когда шпионил объект java. net .URL?

Mocked type must be the same as the type of your spied instance.
Mocked type must be: URL, but is: URL$$PowerMock0

Спасибо.


package com.abc;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;

import java.net.URL;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
//@PrepareForTest({URL.class})
public class ITest1 {
    @Test
    public void test1() throws Exception {
        URL url1 = new URL("http://localhost:1234/path");
        URL url = PowerMockito.spy(url1);
        System.out.println(url);
    }
}

org.mockito.exceptions.base.MockitoException: 
Mocked type must be the same as the type of your spied instance.
Mocked type must be: URL, but is: URL$$PowerMock0
  //correct spying:
  spy = mock( ->ArrayList.class<- , withSettings().spiedInstance( ->new ArrayList()<- );
  //incorrect - types don't match:
  spy = mock( ->List.class<- , withSettings().spiedInstance( ->new ArrayList()<- );

    at com.abc.ITest1.test1(ITest1.java:19)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Невозможно использовать org.mockito.Mockito.spy (URL), поскольку это приводит к ошибке:

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class java.net.URL
Mockito cannot mock/spy because :
 - final class

Использование Mockito 2.15 и Powermock 2.0.7.

+- org.mockito:mockito-core:jar:2.15.0:test
|  +- net.bytebuddy:byte-buddy:jar:1.7.11:test
|  +- net.bytebuddy:byte-buddy-agent:jar:1.7.11:test
|  \- org.objenesis:objenesis:jar:2.6:test
+- org.powermock:powermock-api-mockito2:jar:2.0.7:test
|  \- org.powermock:powermock-api-support:jar:2.0.7:test
+- org.powermock:powermock-core:jar:2.0.7:test
|  +- org.powermock:powermock-reflect:jar:2.0.7:test
|  \- org.javassist:javassist:jar:3.27.0-GA:runtime
+- org.powermock:powermock-module-junit4:jar:2.0.7:test
|  \- org.powermock:powermock-module-junit4-common:jar:2.0.7:test

Обновление:

enter image description here

...