在Java中使用mockito库模拟最终的类

问题描述:

我有一个类是最终的,它有一个我想要执行某些操作的方法。正因为如此,我想创建最终课程的对象。但我无法创建它,下面是我的课程。在Java中使用mockito库模拟最终的类

public final class A { 

    private String name; 

    A(String name){ 
     this.name = name; 
    } 

    public String getName(){ 
     return name; 
    } 
} 

在我的JUnit测试情况下,我想创建一个类的对象,像下面

Class TestA{ 

     @Test 
     public void testA(){ 
      A a = mock(A.class); 

      when(a.getName()).then("ABC"); //on this line i am getting exception 
     } 
} 

我以新的关键字也有,但是没有工作尝试过。那么无论如何要创建一个最终课程的模拟对象?

继例外,我面对的,

org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class A 
Mockito cannot mock/spy following: 
    - final classes 
    - anonymous classes 
    - primitive types 
    at com.rocket.map.resources.TestA.testA(TestA.java:46) 
    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:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 
+0

为什么你需要嘲笑那个班?你为什么不使用它? – tddmonkey

+0

为什么要嘲笑它?为什么不直接用名称'“ABC”'实例化? – khelwood

+0

在实例化模拟库上抛出异常。 –

尝试使用此。

Use the @RunWith(PowerMockRunner.class) annotation at the class-level of the test case. 
Use the @PrepareForTest(ClassWithFinal.class) annotation at the class-level of the test case. 
Use PowerMock.createMock(ClassWithFinal.class) to create a mock object for all methods of this class (let's call it mockObject). 
Use PowerMock.replay(mockObject) to change the mock object to replay mode. 
Use PowerMock.verify(mockObject) to change the mock object to verify mode. 

也可参考此答案 - link

而且Tutorial

两者看起来都很容易实现。

这是不可能的V1的Mockito请 看看这个link。我认为在预先版本或powermockito你可以做到这一点。Powermockito example

+1

当推荐Powermock时,应该总是添加Powermock与字节码混淆的一小部分信息。 – Turing85