我还有最后一节课,大概是这样的:

public final class RainOnTrees{

   public void startRain(){

        // some code here
   }
}

我在其他一些类中使用这个类,像这样:

public class Seasons{

   RainOnTrees rain = new RainOnTrees();

   public void findSeasonAndRain(){

        rain.startRain();

    }
}

在我的JUnit测试类Seasons.java中,我想模拟RainOnTrees类。我怎么能用Mockito做到这一点?


当前回答

我克服了这条信息:

org.mockito.exceptions.base.MockitoException: 不能模拟/间谍类org.slf4j.impl.Log4jLoggerAdapter Mockito不能模仿/侦察,因为: 最终类或匿名类

从这个:log = spy(log);

用这个代替:

log = mock(Logger.class);

这样就有用了。

我猜“默认”记录器适配器是final类的一个实例,所以我不能“侦察”它,但我可以模拟整个事情。去图…

这可能意味着你可以用其他一些“非最终”实例来代替它,如果你有的话。或者简化版本等等。就其价值而言……

其他回答

是的,同样的问题在这里,我们不能模拟一个最终类与Mockito。准确地说,Mockito不能模仿/监视以下内容:

最后的课程 匿名类 原始类型

但是在我看来,使用包装器类代价很大,所以改用PowerMockito。

我正在编写我在Java 11中模拟final/private类及其方法的各种失败尝试之后所遵循的步骤,这些尝试最终对我有用。

在里面创建一个名为org.mockito.plugins.MockMaker的文件 您的test/resources/mockito-extensions文件夹。请创建 Mockito-extensions文件夹(如果还没有)。 在上面的org.mockito.plugins.MockMaker文件中添加一行mock-maker-inline内容 添加

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.management.*", "jdk.internal.reflect.*", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
@PrepareForTest(Utility.class)

类级别的注释。

在测试类中设置过程

  @Before
  public void setup () {
    MockitoAnnotations.initMocks(this);
    Mockito.mockStatic(ClassToBeMocked.class); 
  }

使用Mockito.when(..).thenReturn(..)进行断言 在多个测试用例的情况下,添加下面的代码

  @After
  public void after() {
        Mockito.framework().clearInlineMocks();
  }

我正在使用的模拟版本:3.9.0 Java版本:11

Mockito 2现在支持final类和方法!

但就目前而言,这只是一个“孵化”功能。它需要一些步骤来激活它,这些步骤在Mockito 2中的新内容中描述:

Mocking of final classes and methods is an incubating, opt-in feature. It uses a combination of Java agent instrumentation and subclassing in order to enable mockability of these types. As this works differently to our current mechanism and this one has different limitations and as we want to gather experience and user feedback, this feature had to be explicitly activated to be available ; it can be done via the mockito extension mechanism by creating the file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker containing a single line: mock-maker-inline After you created this file, Mockito will automatically use this new engine and one can do : final class FinalClass { final String finalMethod() { return "something"; } } FinalClass concrete = new FinalClass(); FinalClass mock = mock(FinalClass.class); given(mock.finalMethod()).willReturn("not anymore"); assertThat(mock.finalMethod()).isNotEqualTo(concrete.finalMethod()); In subsequent milestones, the team will bring a programmatic way of using this feature. We will identify and provide support for all unmockable scenarios. Stay tuned and please let us know what you think of this feature!

为运行mockito成功添加这些依赖项:

testImplementation org.mockito: mockito-core: 2.24.5 testImplementation“org.mockito: mockito-inline: 2.24.5”

在Mockito 3和更多的我有同样的问题,并修复它从这个链接

模拟最终类和方法与Mockito 依下列各项

在Mockito用于模拟最终类和方法之前,需要对它进行>配置。 我们需要在项目的src/test/resources/mockito-extensions目录中添加一个名为org.mockito.plugins.MockMaker的文本文件,并添加一行文本: mock-maker-inline Mockito在加载时检查扩展目录中的配置文件。该文件允许模拟final方法和类。