EasyMock:我如何在没有警告的情况下创建泛型类的模拟? [英] EasyMock: How do I create a mock of a genericized class without a warning?

查看:161
本文介绍了EasyMock:我如何在没有警告的情况下创建泛型类的模拟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码

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

给我一​​个警告类型安全性:SomeClass类型的表达式需要未经检查的转换符合SomeClass<整数>。

解决方案

AFAIK,当涉及类名字面值时,你无法避免未经检查的警告, code> SuppressWarnings 注解是处理这种情况的唯一方法。



请注意,缩小 SuppressWarnings 注释尽可能多。您可以将此批注应用于单个局部变量赋值:

  public void testSomething(){

@SuppressWarnings(unchecked)
Foo<整数> foo = EasyMock.createMock(Foo.class);

//其余的测试方法可能还会暴露其他警告

code $ $ $ $ $ $ $ $ $ $ $ $ $ 使用辅助方法:

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

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

//其余的测试方法仍可能会暴露其他警告

code $ <$ pre

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

解决方案

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
}

这篇关于EasyMock:我如何在没有警告的情况下创建泛型类的模拟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆