Java 如何模拟正在测试的同一类中的另一个方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23958611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:24:51  来源:igfitidea点击:

How to mock another method in the same class which is being tested?

javaunit-testingjunitmockito

提问by Rishabh

I am writing JUnit Test case for a class which has two methods methodA,methodB. I would like to mock the call to the methodB from methodA in my test case I am using spy on the class which I am testing, but still the methodB gets executed.

我正在为一个具有两个方法 methodA、methodB 的类编写 JUnit 测试用例。我想在我的测试用例中模拟从 methodA 对 methodB 的调用我在我正在测试的类上使用 spy,但仍然执行 methodB。

here is the class

这是课程

 public class SomeClass
{

     public Object methodA(Object object) 
    {
         object=methodB(object);

        return object;
    }



    public Object methodB(Object object) 
    {
        //do somthing
        return object;
    }
}

here is the test class

这是测试类

  @RunWith( org.powermock.modules.junit4.legacy.PowerMockRunner.class )
    @PrepareForTest(SomeClass.class)
    public class SomeClassTest { 

        private SomeClass var = null; 


        @Before
        public void setUp() {

            var=new SomeClass();

        }

        @After
        public void tearDown()
            {
            var= null;

        }

        @Test
        public void testMethodA_1()
            throws Exception {
            Object object =new Object();
        SomeClass spy_var=PowerMockito.spy(var);
        PowerMockito.when(spy_var.methodB(object)).thenReturn(object);

    Object result = var.methodA(object);        

        assertNotNull(result);

        }

    }

The method B still gets the called though I have mocked it PLease suggest me a solution with the proper way of mocking the methodB call from methodA of the same class.

尽管我已经嘲笑了方法 B 仍然被调用,但请建议我一个解决方案,以正确的方式模拟来自同一类的 methodA 的 methodB 调用。

采纳答案by Totoro

I ran into this yesterday, for spies is best to do doReturn(X).when(spy).method(any())

我昨天遇到了这个,间谍最好做 doReturn(X).when(spy).method(any())

回答by Sam Holder

Taking this approach will result in brittle tests which will need to change if you refactor your class under test. I would highly recommend that you try to assert your expected test results by checking state of SomeClassrather than relying on mocks.

采用这种方法将导致脆弱的测试,如果你重构你的被测类,这些测试将需要改变。我强烈建议您尝试通过检查状态SomeClass而不是依赖模拟来断言预期的测试结果。

If you do indeed need to mock MethodBthen this is an indication that maybe the behaviour in MethodBactually belongs in a separate class which you could then test the interaction of SomeClasswith via mocks

如果您确实需要模拟,MethodB那么这表明可能 中的行为MethodB实际上属于一个单独的类,然后您可以SomeClass通过模拟测试与

if you do indeed need to do what you ask for then a PartialMock is what you want.

如果你确实需要做你所要求的,那么 PartialMock 就是你想要的。

you probably want to create a partial mock of some class but indicate that calls to MethodAshould call the actual method but then mock MethodB

您可能想要创建某个类的部分模拟,但表明调用MethodA应该调用实际方法,然后模拟MethodB

You can see how to use them in the Mockito documentation

你可以在Mockito 文档中看到如何使用它们

As stated in their documentation though Partial mocks are a code smell, though they have identified some explicit use cases.

正如他们的文档中所述,虽然 Partial mocks 是一种代码味道,但他们已经确定了一些明确的用例。