我正在为项目编写 Junit jupiter测试。我正在尝试使用模拟来模拟新实例的创建。但是我没有得到模拟对象,而是得到了实际的对象
请在下面查看我的代码
主要的java类
public class TestSubjectClass {
public String doSomething() {
Integer number = new Integer(1);
return internalLogic(number.toString());
}
}
我的测试班
@RunWith(PowerMockRunner.class)
@PrepareForTest(TestSubjectClass.class)
class TestSubjectClassTest {
@Test
public void mockNewObjectCreation() throws Exception {
TestSubjectClass testedClass = new TestSubjectClass();
Integer mockedValue = new Integer(5);
PowerMockito.whenNew(Integer.class).withAnyArguments().thenReturn(mockedValue);
String output = testedClass.doSomething();
assertThat(output, CoreMatchers.containsString("Here is an input: 5"));
}
}
我的 pom.xml
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
当我运行 junit 5 测试时,新实例的实际模拟没有发生。任何线索我做错了什么
Java.lang.assertionerror:预期:包含“这里是输入 5”但为“这里是输入 1”的字符串
错误: