当前位置: 首页 > 知识库问答 >
问题:

java中使用mockito库的Mock final类[duplicate]

屠杰
2023-03-14
public final class A {

     private String name;

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

     public String getName(){
       return name;
     }
}
 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)

共有1个答案

李烨
2023-03-14

试试用这个。

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.

另请参阅此应答链接

和教程。

 类似资料:
  • 问题内容: 我想测试一个抽象类。当然,我可以手动编写一个从类继承的模拟。 我可以使用模拟框架(我正在使用Mockito)来执行此操作,而不是手工制作模拟吗?怎么样? 问题答案: 以下建议让你在不创建“真实”子类的情况下测试抽象类-Mock 是子类。 使用,然后模拟所有被调用的抽象方法。 例: 注意:此方法的好处是,你不具备实现的抽象方法,只要他们永远不会被调用。 在我看来,这比使用间谍更整洁,因为

  • 问题内容: 我有一个旧类,其中包含用于实例化LoginContext()的new()调用: 我想使用Mockito测试类来模拟LoginContext,因为它要求在实例化之前设置JAAS安全性,但是我不确定如何在不更改login()方法以外部化LoginContext的情况下进行此操作。是否可以使用Mockito模拟LoginContext类? 问题答案: 对于将来,我会推荐伊兰·哈雷尔(Eran

  • 引用同一类中的模拟方法 测试类 当调用真正的类temp到methodA时,应该返回模拟的方法B的值。从而返回true。为什么这不正确。我遇到了同样的问题。我想在真实的类上运行测试,而不是像答案中建议的那样在模拟对象上运行测试。我想运行类methodA,并在调用它时期待模拟对象spyTemp methodB的值

  • 本文向大家介绍mockito 使用Mockito批注,包括了mockito 使用Mockito批注的使用技巧和注意事项,需要的朋友参考一下 示例 我们要测试的类是: 它的合作者是: 在我们的测试中,我们想打破依赖关系Collaborator及其错误,因此我们将模拟Collaborator。使用@Mock注释是为每个测试创建不同的模拟实例的便捷方法: Mockito将尝试按以下顺序解决依赖项注入:

  • 问题内容: 我有一个使用当前时间进行一些计算的函数。我想使用模仿器模拟它。 我要测试的课程示例: 我想要类似的东西: 可以嘲笑吗?我不想更改“已测试”的代码以进行测试。 问题答案: 正确的做法是重组代码,使其更具可测试性,如下所示。重组代码以消除对Date的直接依赖关系将使您可以为正常运行时和测试运行时注入不同的实现:

  • 问题内容: 我是开发的新手,尤其是单元测试的新手。我想我的要求很简单,但是我很想知道其他人对此的想法。 假设我有两个这样的类- 假设我正在编写单元测试到测试方法。但是,假设我想像这样模拟课程。我正在使用Mockito来做到这一点。 我看到模拟未生效,并且断言失败。没有办法模拟我要测试的类的成员变量。? 问题答案: 您需要提供一种访问成员变量的方式,以便您可以进行模拟传递(最常见的方式是使用参数的s