- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
描述:
我似乎无法让我的 stub 或模拟在我接受测试的类(class)中生效。我正在尝试使用 whenNew 操作,以便我可以模拟返回对象,然后使用返回值模拟对该对象的操作。
我想象它是一些简单的东西,我错过了但没有看到它。
解决方案:最初我使用 MockitoRunner.class
运行,需要将其更改为 PowerMockRunner.class
。下面的代码反射(reflect)了解决方案。
类路径上的 jar :
测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import static org.powermock.api.mockito.PowerMockito.*;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.any;
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassA.class)
public class ClassATest {
@Test
public void test() throws Exception
{
String[] returnSomeValue = {"PowerMockTest"};
String[] inputValue = {"Test1"};
ClassB mockedClassB = mock(ClassB.class);
whenNew( ClassB.class).withNoArguments().thenReturn( mockedClassB );
when( mockedClassB, "getResult", any(String[].class) ).thenReturn(returnSomeValue);
IClassA classUnderTest = new ClassA();
String[] expectedValue = classUnderTest.runTest(inputValue);
}
}
A 级实现
public class ClassA implements IClassA {
@Override
public String[] runTest(String[] inputValues) {
String[] result;
IClassB classB = new ClassB();
result = classB.getResult(inputValues);
return result;
}
}
最佳答案
由于您正在使用 powermock 功能(@PrepareForTest
、PowerMockito.whenNew
等),因此您必须使用 PowerMockRunner 运行测试。
@RunWith(PowerMockRunner.class)
因为 ClassB#geResult 不是私有(private)的,你也可以简化你的代码并替换
when( mockedClassB, "getResult", any(String[].class) ).thenReturn(someValue);
通过
when(mockedClassB.getResult(any(String[].class))).thenReturn(someValue);
关于java - PowerMockito 模拟 whenNew 没有生效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11957485/
我的测试方法中有一个构造函数调用。我想模拟它,因为尝试启动一个用户界面。我也模拟了构造函数的输入,但它仍然试图创建实际实例。 生产代码: public void methodToTest() {
我正在尝试使用 PowerMock 来取消对 Jackson ObjectMapper 的调用,但由于某种原因它不起作用,我怀疑它与 whenNew 在被测试的方法中实例化时实际上没有提供模拟实例有关
我即将构建一个 JSON 对象并想要测试结果。我正在调用一个公共(public)方法。有几个私有(private)方法并进行递归调用。在开始的某个地方我有这个: JSONObject obj = ne
在Powermock中有方法whenNew。 我是 Jmockit 新手。我正在将我的应用程序从 Powermock 迁移到 Jmockit。 您能否告诉我 Jmockit 中 whenNew 的等效
更新。最后检查工作示例。 我有一个类: package test; public class ClassXYZ { private final String message; publ
大家好,我是 PowerMockito 的新手,我正在尝试在 PoweMockito 中使用 whenNew,但它对我不起作用,有人可以帮我解决这个问题吗?? 下面是我用于测试 Class2 的测试方
我有一个类似于下面的代码: Class A { public boolean myMethod(someargs) { MyQueryClass query = new MyQueryCl
我有一些 API 的实现。 例如: public class SoPostApi implements SoApi { ... @Override public void searchOrganiza
在我无法重构的源类中(因此我无法使用建议 here ),存在带有 = new XXX 的对象创建。我必须模拟他们的函数调用 X().call()。 为此,我使用 powermock 的 whenNew
这是我的情况,我有 2 个非常简单的类: public class B { public void doSomething(){ System.out.println("doS
我希望 PowerMockito 返回我的空数组列表 Foo什么时候new ArrayList()被称为,但我不确定如何构造语句。具体来说,我想要 new ArrayList()照常创建新列表。 Ar
描述: 我似乎无法让我的 stub 或模拟在我接受测试的类(class)中生效。我正在尝试使用 whenNew 操作,以便我可以模拟返回对象,然后使用返回值模拟对该对象的操作。 我想象它是一些简单的东
我不知道它是否应该这样做,但我想不会。看看下面我的代码。 File mocked = PowerMockito.mock(File.class); PowerMockito.whenNew(Fi
我有一个类似下面的代码: Class A { public boolean myMethod(someargs) { MyQueryClass query = new MyQueryCla
我正在尝试使用模拟 Intent 对象,但是当使用 PowerMockito 中的 whenNew 时,我无法模拟构造函数。我已经尝试了所有可能的参数组合,但它不起作用。 最佳答案 我遇到了类似的问题
我有以下测试类: @RunWith(PowerMockRunner.class) @PrepareForTest({BuildCcHeaderProcess.class, IOUtils.class}
我正在尝试更新现有代码以删除 PowerMockito 并将其替换为 Mockito,因为 Mockito 支持模拟静态和模拟构造。 我知道您可以执行以下操作: assertEquals("foo"
我想测试以下类(class): public class TestClass { private OuterClass client; public TestClass(int v1,
我有一个较大的 Maven 多项目构建,单元测试主要使用 PowerMock,版本为 1.6.2。我决定集成 JaCoCo,这样我们就可以开始发布我们的代码覆盖率指标。 JaCoCo 的最新版本 0.
我是一名优秀的程序员,十分优秀!