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

发布于 2024-07-04 07:19:47 字数 211 浏览 10 评论 0原文

该代码

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

向我发出警告“类型安全:SomeClass 类型的表达式需要未经检查的转换才能符合 SomeClass”。

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>".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

再见回来 2024-07-11 07:19:47

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

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

public void testSomething() {

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

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

或使用辅助方法:

@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
}

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

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

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
}
迷途知返 2024-07-11 07:19:47

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

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

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

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

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);
吹泡泡o 2024-07-11 07:19:47

两个明显的途径是抑制警告或模拟子类。

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

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

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.)

美男兮 2024-07-11 07:19:47

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

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

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.

@Test
@SuppressWarnings("unchecked")
public void someTest() {
    SomeClass<Integer> someClass = EasyMock.createMock(SomeClass.class);
}
酒废 2024-07-11 07:19:47

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

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

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

代替

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文