Java EasyMock:如何在没有警告的情况下创建泛化类的模拟?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56954/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:44:31  来源:igfitidea点击:

EasyMock: How do I create a mock of a genericized class without a warning?

javagenericsmockingeasymock

提问by Kevin Wong

The code

编码

private SomeClass<Integer> someClass;
someClass = EasyMock.createMock(SomeClass.class);

gives me a warning "Type safety: The expression of type SomeClass needs unchecked conversion to conform to SomeClass<Integer>".

给我一个警告“类型安全:SomeClass 类型的表达式需要未经检查的转换以符合 SomeClass<Integer>”。

回答by Tom Hawtin - tackline

The two obvious routes are to suppress the warning or mock a subclass.

两条明显的路线是抑制警告或模拟子类。

private static class SomeClass_Integer extends SomeClass<Integer>();
private SomeClass<Integer> someClass;
...
    someClass = EasyMock.createMock(SomeClass_Integer.class);

(Disclaimer: Not even attempted to compile this code, nor have I used EasyMock.)

(免责声明:我什至没有尝试编译这段代码,也没有使用过 EasyMock。)

回答by Cem Catikkas

You can annotate the test method with @SuppressWarnings("unchecked"). I agree this is some what of a hack but in my opinion it's acceptable on test code.

您可以使用 注释测试方法@SuppressWarnings("unchecked")。我同意这是一种黑客行为,但在我看来,它在测试代码中是可以接受的。

@Test
@SuppressWarnings("unchecked")
public void someTest() {
    SomeClass<Integer> someClass = EasyMock.createMock(SomeClass.class);
}

回答by Barend

AFAIK, you can't avoid the unchecked warning when a class name literal is involved, and the SuppressWarningsannotation is the only way to handle this.

AFAIK,当涉及类名文字时,您无法避免未经检查的警告,并且SuppressWarnings注释是处理此问题的唯一方法。

Note that it is good form to narrow the scope of the SuppressWarningsannotation as much as possible. You can apply this annotation to a single local variable assignment:

请注意,尽可能缩小SuppressWarnings注释的范围是一种很好的形式。您可以将此注释应用于单个局部变量赋值:

public void testSomething() {

    @SuppressWarnings("unchecked")
    Foo<Integer> foo = EasyMock.createMock(Foo.class);

    // Rest of test method may still expose other warnings
}

or use a helper method:

或使用辅助方法:

@SuppressWarnings("unchecked")
private static <T> Foo<T> createFooMock() {
    return (Foo<T>)EasyMock.createMock(Foo.class);
}

public void testSomething() {
    Foo<String> foo = createFooMock();

    // Rest of test method may still expose other warnings
}

回答by Barry John Williams

I worked around this problem by introducing a subclass, e.g.

我通过引入一个子类来解决这个问题,例如

private abstract class MySpecialString implements MySpecial<String>{};

Then create a mock of that abstract class:

然后创建该抽象类的模拟:

MySpecial<String> myMock = createControl().createMock(MySpecialString.class);

回答by chim

I know this goes against the question, but why not create a List rather than a Mock List?

我知道这与问题背道而驰,但为什么不创建一个列表而不是一个模拟列表?

It's less code and easier to work with, for instance if you want to add items to the list.

它的代码更少且更易于使用,例如,如果您想将项目添加到列表中。

MyItem myItem = createMock(myItem.class);
List<MyItem> myItemList = new ArrayList<MyItem>();
myItemList.add(myItem);

Instead of

代替

MyItem myItem = createMock(myItem.class);
@SuppressWarnings("unchecked")
List<MyItem> myItemList = createMock(ArrayList.class);
expect(myItemList.get(0)).andReturn(myItem);
replay(myItemList);