如何使用 Mockito 在类中只模拟一个静态方法? [英] How to mock just one static method in a class using Mockito?

查看:244
本文介绍了如何使用 Mockito 在类中只模拟一个静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下行似乎模拟了类中的所有静态方法:

MockedStatic sampleMock = Mockito.mockStatic( Sample.class );sampleMock.when( () -> Sample.sampleStaticMethod( Mockito.any( String.class ) ) ).thenReturn( "response" );

是否可以只模拟一个类中的一个静态方法?

解决方案

默认情况下所有方法都是模拟的.但是,使用 Mockito.CALLS_REAL_METHODS 您可以配置模拟以实际触发真正的方法,仅排除一个.

例如给定类Sample:

class Sample{静态字符串方法1(字符串s){返回 s;}静态字符串方法2(字符串s){返回 s;}}

如果我们只想模拟 method1:

@Testpublic void singleStaticMethodTest(){尝试 (MockedStatic 嘲笑 = Mockito.mockStatic(Sample.class,Mockito.CALLS_REAL_METHODS)) {mocked.when(() -> Sample.method1(anyString())).thenReturn(bar");assertEquals(bar", Sample.method1(foo"));//模拟assertEquals(foo", Sample.method2(foo"));//没有被嘲笑}}


注意真正的 Sample.method1() 仍然会被调用.来自 Mockito.CALLS_REAL_METHODS 文档:

<块引用>

这种实现在处理遗留代码时很有帮助.什么时候使用此实现,未存根的方法将委托给实际执行.这是一种创建部分模拟对象的方法默认情况下调用真正的方法....

<块引用>

注意 1:使用when(mock.getSomething()).thenReturn(fakeValue) 语法会调用真正的方法.对于部分模拟,建议使用 doReturn语法.

因此,如果您根本不想触发存根静态方法,则解决方案是使用语法 doReturn (如文档所建议的),但仍然不支持静态方法:

@Testpublic void singleStaticMethodTest() {尝试 (MockedStatic 嘲笑 = Mockito.mockStatic(Sample.class,Mockito.CALLS_REAL_METHODS)) {doReturn("bar").when(mocked).method1(anyString());//编译错误!//...}}

关于这个,有一个未决问题,特别检查这个评论.

The following line seems to mock all static methods in the class:

MockedStatic <Sample> sampleMock = Mockito.mockStatic( Sample.class );
sampleMock.when( () -> Sample.sampleStaticMethod( Mockito.any( String.class ) ) ).thenReturn( "response" );

Is it possible to mock just one static method in a class?

解决方案

By default all methods are mocked. However, using Mockito.CALLS_REAL_METHODS you can configure the mock to actually trigger the real methods excluding only one.

For example given the class Sample:

class Sample{
    static String method1(String s) {
        return s;
    }
    static String method2(String s) {
        return s;
    }
}

If we want to mock only method1:

@Test
public void singleStaticMethodTest(){
    try (MockedStatic<Sample> mocked = Mockito.mockStatic(Sample.class,Mockito.CALLS_REAL_METHODS)) {
        mocked.when(() -> Sample.method1(anyString())).thenReturn("bar");
        assertEquals("bar", Sample.method1("foo")); // mocked
        assertEquals("foo", Sample.method2("foo")); // not mocked
    }
}


Be aware that the real Sample.method1() will still be called. From Mockito.CALLS_REAL_METHODS docs:

This implementation can be helpful when working with legacy code. When this implementation is used, unstubbed methods will delegate to the real implementation. This is a way to create a partial mock object that calls real methods by default. ...

Note 1: Stubbing partial mocks using when(mock.getSomething()).thenReturn(fakeValue) syntax will call the real method. For partial mock it's recommended to use doReturn syntax.

So if you don't want to trigger the stubbed static method at all, the solution would be to use the syntax doReturn (as the doc suggests) but for static methods is still not supported:

@Test
public void singleStaticMethodTest() {
    try (MockedStatic<Sample> mocked = Mockito.mockStatic(Sample.class,Mockito.CALLS_REAL_METHODS)) {
        doReturn("bar").when(mocked).method1(anyString()); // Compilation error!
        //...
    }
}

About this there is an open issue, in particular check this comment.

这篇关于如何使用 Mockito 在类中只模拟一个静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆