java PowerMockito.whenNew 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43542690/
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-11-03 07:31:31  来源:igfitidea点击:

PowerMockito.whenNew is not working

javajunitpowermockito

提问by kaushik

Hi folks i am new to PowerMockito and i am trying to use whenNew in PoweMockito and its not working for me, can anyone please help me out with this??

大家好,我是 PowerMockito 的新手,我正在尝试在 PoweMockito 中使用 whenNew 但它对我不起作用,有人可以帮我解决这个问题吗?

Below is my Test method which is used to test Class2, and i have Used PowerMockito.whenNew for mocking mockTestMethod inside Class2 and return String Value as "MOCKED VALUE" but that is not happening and actually the method is being executed and output is "PassedString". If i am not wrong the Output should have string as "Inside Class2 method MOCKED VALUE" but i am getting output as "Inside Class2 method PassedString." Please help me with the issue, Thanks In Advance.

下面是我用于测试 Class2 的 Test 方法,我使用 PowerMockito.whenNew 来模拟 Class2 中的 mockTestMethod 并将字符串值返回为“MOCKED VALUE”,但这并没有发生,实际上正在执行该方法并且输出是“PassedString ”。如果我没有错,输出应该有字符串为“Inside Class2 method MOCKED VALUE”,但我得到的输出为“Inside Class2 method PassedString”。请帮我解决这个问题,提前致谢。

Below is the complete program which i am working on

以下是我正在开发的完整程序

package com.hpe.testing2;

public class Class2 {

    public void testingMethod(){
        Class1 class1 = new Class1();
        String result = class1.mockTestMethod("PassedString");
        System.out.println("Inside Class2 method " + result);
    }

}

package com.hpe.testing2;

public class Class1 {

    public String mockTestMethod(String str2){
        String str1="SomeString";
        str1 = str2;
        System.out.println("Inside MockTest Method " + str1);
        return str1;
    }

}

class2 is invoking Class1 mockTestMethod internally as shown above.

class2 正在内部调用 Class1 mockTestMethod,如上所示。

package com.hpe.testing2;


import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


@RunWith(PowerMockRunner.class)
@PrepareForTest({Class2.class,Class1.class})
public class ClassTest {

    public static void main(String[] args) throws Exception {
        ClassTest testing = new ClassTest();
        testing.runMethod();
    }

    public void runMethod() throws Exception{
        Class2 class2 = new Class2();
        Class1 class1 = PowerMockito.mock(Class1.class);
        PowerMockito.whenNew(Class1.class).withAnyArguments().thenReturn(class1);
        PowerMockito.when(class1.mockTestMethod(Mockito.anyString())).thenReturn("MOCKED
 VALUE");
        class2.testingMethod();
    }

}

回答by SilverNak

You cannot start a test class via mainmethod. Instead it should be run with JUnit. Therefore a @Testannotation has to be present at the test method. Look herefor getting started with JUnit.

您不能通过main方法启动测试类。相反,它应该与 JUnit 一起运行。因此@Test,测试方法中必须存在注释。看这里开始使用 JUnit。

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Class2.class, Class1.class })
public class ClassTest {

    @Test
    public void runMethod() throws Exception {
        Class2 class2 = new Class2();
        Class1 class1 = PowerMockito.mock(Class1.class);

        PowerMockito.whenNew(Class1.class).withAnyArguments().thenReturn(class1);
        PowerMockito.when(class1.mockTestMethod(Mockito.anyString())).thenReturn("MOCKED VALUE");
        class2.testingMethod();
    }

}

(I left out imports in your testclass)

(我在你的测试类中省略了导入)