我正在尝试测试一个方法是否创建一个对象。我几乎可以使用 PowerMockito.verifyNew().withArguments() 来工作,但是,传递给构造函数的参数是一个对象和一个对象的 ArrayList。测试的输出是:实际
invocationSubstitute.performSubstitutionLogic(
1,
6,
11,
13,
[au.edu.sccs.csp3105.NBookingPlanner.Person@2449cff7],
au.edu.sccs.csp3105.NBookingPlanner.Room@62da83ed,
"description"
);
Run Code Online (Sandbox Code Playgroud)
预期的
invocationSubstitute.performSubstitutionLogic(
1,
6,
11,
13,
[au.edu.sccs.csp3105.NBookingPlanner.Person@40bffbca],
au.edu.sccs.csp3105.NBookingPlanner.Room@42a9a63e,
"description"
);
Run Code Online (Sandbox Code Playgroud)
我可以看到问题是对象是相同类型但不是同一对象,有没有办法说预期对象是正确的类型?
测试:
@RunWith(PowerMockRunner.class)
@PrepareForTest({Planner.class, Meeting.class})
public class MonthInput {
Planner planner;
@Rule
public final TextFromStandardInputStream systemInMock = emptyStandardInputStream();
@Rule
public final ExpectedSystemExit exit = ExpectedSystemExit.none();
@SuppressWarnings("deprecation")
@Before
public void setup() throws Exception {
Meeting meetingMock = Mockito.mock(Meeting.class);
PowerMockito.whenNew(Meeting.class).withAnyArguments().thenReturn(meetingMock);
}
@Test
public void MonthInputofless5() throws Exception {
// make spy
planner = Mockito.spy(Planner.class);
//override main menu with do …Run Code Online (Sandbox Code Playgroud) `java.lang.LinkageError: loader constraint violation: when resolving overridden method "com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshal
`Exception(Lorg/xml/sax/SAXException;)Ljavax/xml/bind/UnmarshalException;" the class loader (instance of org/powermock/core/classloader/MockClassLoader) of the current class, com/sun/xml/bind/v2/runtime/unmarshaller/UnmarshallerImpl, and its superclass loader (instance of <bootloader>), have different Class objects for the type org/xml/sax/SAXException used in the signature
Run Code Online (Sandbox Code Playgroud)
我正在使用 Powermock 在 JUNIT 测试用例中模拟静态类。由于我的 junit 使用 xml 文件,所以我收到了一些错误,我必须使用 powermock 忽略,如下所示。
@PowerMockIgnore({ "javax.xml.*"})
@RunWith(PowerMockRunner.class)
Run Code Online (Sandbox Code Playgroud)
我收到链接错误以及以下错误。请指教
javax.xml.transform.TransformerFactoryConfigurationError: Provider for class javax.xml.transform.TransformerFactory cannot be created
Run Code Online (Sandbox Code Playgroud) MyClass firstClass = PowerMockito.spy(new MyClass());
AnotherClass secondClass;
secondClass = PowerMockito.mock(AnotherClass.class);
PowerMockito.when(secondClass.anotherFunction(Mockito.any()).thenReturn(1);
int myInt = firstClass.myFunction();
if (myInt == 1) {
System.out.println("true");
}
Run Code Online (Sandbox Code Playgroud)
myFunction调用anotherFunction并返回 的结果anotherFunction。
但它并没有像我期望的那样返回1并打印"true“,而是仍在执行其真正的功能。
我在这里缺少什么?
我是junit Mockito框架的新手,我使用powermock框架嘲笑了依赖注入,但是我在@Test注释的eclipse中得到错误错误是"类型不匹配:无法从Test转换为Annotation"
package org.singh.util.MockitoDemo;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import org.junit.*;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ExampleUtil.class, ExamplePojo.class})
public class ExamplePojoTest{
@Test
public void testMethodMakingPrivateMethodCall() throws Exception {
ExamplePojo spyExamplePojo = PowerMockito.spy(new ExamplePojo());
when(spyExamplePojo, method(ExamplePojo.class, "privateMethod", String.class)).withArguments(anyString()).thenReturn("test test");
String result = spyExamplePojo.methodMakingPrivateMethodCall("test");
Assert.assertEquals("test test", result);
}
}
Run Code Online (Sandbox Code Playgroud)
maven依赖是
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Powermock 为以下场景编写 junit。我试图编写示例代码,而不是复制不允许在此处发布的确切代码。
class MainClass{
First.getC().setStr("abc");
}
final class First{
public static ClassC getC() {
return c;
}
}
class ClassC{
private String str;
//getter/setter for str
}
Run Code Online (Sandbox Code Playgroud)
它总是失败。我的junit如下:
@RunWith(SpringJUnit4ClassRunner.class)
public class MainClassTest {
@Spy MainClass;
@Mock
private ClassC classc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
PowerMockito.mockStatic(First.class);
}
@Test
public void myTest(){
when(First.getC()).thenReturn(classc);
Mockito.doCallRealMethod().when(classc).setStr(Mockito.any(String.class))
}
}
Run Code Online (Sandbox Code Playgroud) 作为副本提出的解决方案不是 PowerMockito 解决方案,因此不能回答这个问题。此外,这个问题在下面得到了合理的回答。
IDK 是否是重复的,但如果是重复的,我肯定找不到相关项目。我一直期待这真的很简单,因为反射很简单,但我宁愿使用正确的工具来做。
说明:遗留代码。没有吸气剂/二传手。
为此使用 Whitebox 是否正确?我认为它是“Off Limits”,即内部 API 的一部分?……还是严格意义上的 Mockito?
我正在尝试对使用java.time.LocalDateTime. 我能够让模拟工作,但是当我增加时间(分钟或天)时,我最终会得到一个null值。
@RunWith(PowerMockRunner.class)
@PrepareForTest({ LocalDateTime.class })
public class LocalDateTimeMockTest
{
@Test
public void shouldCorrectlyCalculateTimeout()
{
// arrange
PowerMockito.mockStatic(LocalDateTime.class);
LocalDateTime fixedPointInTime = LocalDateTime.of(2017, 9, 11, 21, 28, 47);
BDDMockito.given(LocalDateTime.now()).willReturn(fixedPointInTime);
// act
LocalDateTime fixedTomorrow = LocalDateTime.now().plusDays(1); //shouldn't this have a NPE?
// assert
Assert.assertTrue(LocalDateTime.now() == fixedPointInTime); //Edit - both are Null
Assert.assertNotNull(fixedTomorrow); //Test fails here
Assert.assertEquals(12, fixedTomorrow.getDayOfMonth());
}
}
Run Code Online (Sandbox Code Playgroud)
我明白(我想我明白)这LocalDateTime是不可变的,我认为我应该得到一个新实例而不是null值。
原来是.of方法给了我一个null价值。为什么?
java ×7
powermockito ×7
junit ×5
powermock ×4
mockito ×3
datetime ×1
java-time ×1
jaxb ×1
junit4 ×1
unit-testing ×1