- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 EasyMock 和 PowerMock 的新手,我可能被困在一些非常基本的东西上。
以下是我想测试的代码
import java.io.File;
public class FileOp() {
private static FileOp instance = null;
public string hostIp = "";
public static FileOp() {
if(null == instance)
instance = new FileOp();
}
private FileOp() {
init();
}
init() {
hostIp = "xxx.xxx.xxx.xxx";
}
public boolean deleteFile(String fileName) {
File file = new File(fileName);
if(file.exists()) {
if(file.delete())
return true;
else
return false;
}
else {
return false;
}
}
}
以下是我的测试代码...
import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import java.io.File;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RunWith(PowerMockRunner.class)
@PrepareForTest(FileOp.class)
public class FileOp_JTest
{
@Test
@PrepareForTest(File.class)
public void deleteFile_Success(){
try {
final String path = "samplePath";
//Prepare
File fileMock = EasyMock.createMock(File.class);
//Setup
PowerMock.expectNew(File.class, path).andReturn(fileMock);
expect(fileMock.exists()).andReturn(true);
expect(fileMock.delete()).andReturn(true);
PowerMock.replayAll(fileMock);
//Act
FileOp fileOp = Whitebox.invokeConstructor(FileOp.class);
assertTrue(fileOp.deleteFile(path));
//Verify
PowerMock.verifyAll();
}
catch (Exception e) {
e.printStackTrace();
assertFalse(true);
}
}
}
测试失败的原因是 assertTrue(fileOp.deleteFile(path));
当调用尝试执行 file.exists() 时,我追踪到了 deleteFile("samplePath") 并且它返回 false。但是,我模拟了 file.exists() 返回 true。
最佳答案
您在测试中使用的文件未被模拟。您有 fileMock,但您的测试中未使用它。您正在测试的方法在以下行中实例化它自己的新 File 对象:
File file = new File(fileName);
如果您的deleteFile方法需要一个File对象而不是一个String,您可以在那里注入(inject)您的mockObject并检查所有调用是否正确。
关于java - PowerMock和EasyMock方法模拟问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9931790/
我有一个具有静态变量、静态 block 和静态函数的公共(public)类。我正在测试我的测试器类中的一个静态函数(比如 x),我通过在测试器类的类级别(Powermock)使用 @SuppressS
我正在尝试编写一个 TestNg 测试用例。这是为了测试当客户端抛出异常时会发生什么。我正在使用 PowerMock 来模拟客户端调用。下面是我的测试方法(只有UnitTest Snipped,而不是
我正在尝试模拟我的测试方法的内部方法调用 我的类(class)看起来像这样 public class App { public Student getStudent() { MyDAO dao
我正在尝试编写一个需要 的测试机器人电动 2.2 和 PowerMock ,因为被测代码依赖于一些 Android 库和第三方库以及我需要模拟的最终类。 鉴于我被迫通过以下方式使用 Robolectr
无论如何,tom 是否使用 Powermock 模拟类中的 super 方法调用? 例如: public void processRetrievedData() { super.processRe
运行Junit测试期间出现以下错误。 java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor at
我想使用 PowerMock 模拟以下代码: ConnectionFactory rabbitMqFactory = createFactory(); com.rabbitmq.client.Conn
我收到 PowerMock 错误,但我没有使用 PowerMocking。我正在使用正常的模拟。这是我要测试的类(class): import java.util.List; import org.a
使用 TestNg + PowerMock + Mockito 时遇到 java.lang.ExceptionInInitializerError: Caused by: java.lang.Null
有没有办法注释@PowerMockIgnore 以忽略整个包,除了这个包中的一个类? 我希望做一些事情,例如: @PowerMockIgnore({ "foo.bar.ignore*\DoNotIgn
当我尝试执行以下简化的 JUnit 测试时,它成功了,但我收到一条错误消息:首先创建所有测试实例时不支持通知! @RunWith(PowerMockRunner.class) @PowerMockRu
我正在测试一些遗留代码并尝试使用 PowerMock 来模拟静态方法调用。我很快发现它与类加载器混淆了,这不是我认为有资格深入研究的问题。仅供引用,我的问题类似于 this但是在我的情况下发布的解决方
public class TestStatic { public static int methodstatic(){ return 3; } } @Test @Pre
我想通过Maven使用最新版本的powermock库(1.6.5)。但是我的包无法编译,因为 Maven 发现依赖收敛错误。下面您可以看到有 2 个不同版本的 org.objenesis:objene
我在 Stack 上看到过这个问题的另一个例子,但没有答案。谁能根据经验(或任何其他深奥的手段)告诉我这是否可能做到?我已经遵循了我能找到的所有模拟静态方法的示例,但还没有找到适用于抽象类中的静态方法
我在模拟 Calendar.getInstance() 时遇到问题。正如您现在一样,此方法返回一个日历 - 我正在 mock 的对象。 现在我的代码如下所示: @RunWith(PowerMockRu
我有一个带有静态方法的简单类,该方法通常会抛出空指针: public class MyClass { private static String s; public static fi
我有多个 HttpPost 请求,如下所示: try (CloseableHttpClient httpclient = HttpClients.createDefault()) { Http
我想通过Maven使用最新版本的powermock库(1.6.5)。但是我的包无法编译,因为 Maven 发现依赖收敛错误。下面您可以看到有 2 个不同版本的 org.objenesis:objene
我正在使用 PowerMock、EasyMock 组合。 如果没有另外指定,如何让每个模拟对象默认返回“null”? 现在,我必须对模拟对象的每个方法进行“预期”(或在mockito世界中的“何时”)
我是一名优秀的程序员,十分优秀!