こんにちは私はPowerMockitoを初めて使用しています。私はPoweMockitoでいつでも使用しようとしていますが、私にとってはうまく動作しません。PowerMockito.whenNewは動作していません
以下は私のTestメソッドで、Class2のテストに使用されています。PowerMockito.whenNewを使用してClass2内のmockTestMethodをモックし、文字列値を「MOCKED VALUE」として返しますが、実際にはメソッドが実行されています出力は "PassedString"です。 もし私が間違っていない場合は、 "Inside Class2 method MOCKED VALUE"のように出力に文字列が必要ですが、 "Class2 method PassedStringの内部"として出力されています。 問題を手伝ってください。 ありがとうございました。
以下は、私が上記のように、内部でのClass1 mockTestMethodを呼び出している
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;
}
}
クラス2に取り組んでいます完全なプログラムです。
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();
}
}
おかげトン...それが働いていました... – kaushik