java 在 JUnit 测试中使用 JMockit 多次模拟静态方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4904041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mock a static method multiple times using JMockit within a JUnit test
提问by pmc255
I have a class with static methods that I'm currently mocking with JMockit. Say it looks something like:
我有一个带有静态方法的类,我目前正在用 JMockit 模拟它。说它看起来像:
public class Foo {
public static FooValue getValue(Object something) {
...
}
public static enum FooValue { X, Y, Z, ...; }
}
I have another class (let's call it MyClass) that calls Foo's static method; I'm trying to write test cases for this class. My JUnit test, using JMockit, looks something like this:
我有另一个类(我们称之为 MyClass),它调用 Foo 的静态方法;我正在尝试为这个类编写测试用例。我的 JUnit 测试,使用 JMockit,看起来像这样:
public class MyClassTest extends TestCase {
@NonStrict private final Foo mock = null;
@Test public void testMyClass() {
new Expectations() {
{
Foo.getValue((Object) any); result = Foo.FooValue.X;
}
};
}
myClass.doSomething();
}
This works fine and dandy, and when the test is executed my instance of MyClass will correctly get the enum value of Foo.FooValue.X when it calls Foo.getValue().
这很好用,当测试执行时,我的 MyClass 实例将在调用 Foo.getValue() 时正确获取 Foo.FooValue.X 的枚举值。
Now, I'm trying to iterate over allthe values in the enumeration, and repeatedly run the test. If I put the above test code in a for loop and try to set the result of the mocked static method to each enumeration value, that doesn't work. The mocked version of Foo.getValue() always returns Foo.FooValue.X, and never any of the other values as I iterate through the enumeration.
现在,我试图遍历枚举中的所有值,并重复运行测试。如果我将上面的测试代码放在 for 循环中并尝试将模拟的静态方法的结果设置为每个枚举值,那将不起作用。Foo.getValue() 的模拟版本总是返回 Foo.FooValue.X,并且在我遍历枚举时从不返回任何其他值。
How do I go about mocking the static method multiple times within the single JUnit test? I want to do something like this (but obviously it doesn't work):
如何在单个 JUnit 测试中多次模拟静态方法?我想做这样的事情(但显然它不起作用):
public class MyClassTest extends TestCase {
@NonStrict private final Foo mock = null;
@Test public void testMyClass() {
for (final Foo.FooValue val : Foo.FooValue.values() {
new Expectations() {
{
// Here, I'm attempting to redefine the mocked method during each iteration
// of the loop. Apparently, that doesn't work.
Foo.getValue((Object) any); result = val;
}
};
myClass.doSomething();
}
}
}
Any ideas?
有任何想法吗?
回答by Rogério
Instead of "mocking the method multiple times", you should record multiple consecutive return values in a single recording:
您应该在单个记录中记录多个连续的返回值,而不是“多次模拟方法”:
public class MyClassTest extends TestCase
{
@Test
public void testMyClass(@Mocked Foo anyFoo)
{
new Expectations() {{
Foo.getValue(any);
result = Foo.FooValue.values();
}};
for (Foo.FooValue val : Foo.FooValue.values() {
myClass.doSomething();
}
}
}
It could also be done with a Delegate
, if more flexibility was required.
Delegate
如果需要更大的灵活性,也可以使用 来完成。