如何用通配符模拟泛型方法的行为 [英] How to mock behavior of generic method with wildcard

查看:152
本文介绍了如何用通配符模拟泛型方法的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用EasyMock(3.2)。我想为基于Spring Security的部分安全系统编写测试。我想嘲笑 身份验证 ,以便它返回空的权限列表。它的方法声明如下:

 集合<扩展GrantedAuthority> getAuthorities(); 

所以我写了一个测试:

 认证认证= createMock(Authentication.class); 
收藏<?扩展GrantedAuthority>权威= Collections.emptyList();
expect(authentication.getAuthorities())andReturn(authorities);

但编译器抱怨和Return call:

类型IExpectationSetters< Collection< capture中的方法和返回(Collection



我做错了什么?






更新:



当我更改权限到:

 集合<授予授权>权限=集合.emptyList(); 

如上所示,它仍然不能编译,但是错误有点不同:

$ b $在类型IExpectationSetters< Collection<捕获#1-of?extends GrantedAuthority>>的类型中的方法和返回(Collection )不是(Collection< GrantedAuthority>)



我确保 GrantedAuthority 在两个声明中都是相同的 - org.springframework.security.core.GrantedAuthority

解决方案

删除该项目从收集声明中键入,你会得到一个警告,但测试将工作。

  @Test 
public void testFoo()
{
// setup
Authentication mockAuthentication = createMock(Authentication.class);
收藏权限= Collections.emptyList();
expect(mockAuthentication.getAuthorities())andReturn(authorities);

//练习
EasyMock.replay(mockAuthentication);
收藏<?扩展GrantedAuthority> retAuth = mockAuthentication.getAuthorities();

// verify
EasyMock.verify(mockAuthentication);
assertEquals(authorities,retAuth);
}


I'm using EasyMock (3.2). I want to write a test for part of my security system based on Spring Security. I want to mock the Authentication so that it returns empty list of authorities. Its method declaration is as follows:

 Collection<? extends GrantedAuthority> getAuthorities();

So I write a test:

Authentication authentication = createMock(Authentication.class);
Collection<? extends GrantedAuthority> authorities = Collections.emptyList();
expect(authentication.getAuthorities()).andReturn(authorities);

But the compiler is complaining about the third line on andReturn call:

The method andReturn(Collection<capture#1-of ? extends GrantedAuthority>) in the type IExpectationSetters<Collection<capture#1-of ? extends GrantedAuthority>> is not applicable for the arguments (Collection<capture#2-of ? extends GrantedAuthority>

What am I doing wrong?


UPDATE:

When I change the declaration of authorities to:

Collection<GrantedAuthority> authorities = Collections.emptyList();

as suggested, it still does not compile, but the error is a bit different:

The method andReturn(Collection<capture#1-of ? extends GrantedAuthority>) in the type IExpectationSetters<Collection<capture#1-of ? extends GrantedAuthority>> is not applicable for the arguments (Collection<GrantedAuthority>)

I ensured that the GrantedAuthority is actually the same in both declarations - org.springframework.security.core.GrantedAuthority.

解决方案

Remove the item type from the collection declaration, you'll get a warning but the test will work.

@Test
public void testFoo()
    {
    // setup
    Authentication mockAuthentication = createMock(Authentication.class);
    Collection authorities = Collections.emptyList();
    expect(mockAuthentication.getAuthorities()).andReturn(authorities);

    // exercise
    EasyMock.replay(mockAuthentication);
    Collection<? extends GrantedAuthority> retAuth = mockAuthentication.getAuthorities();

    // verify
    EasyMock.verify(mockAuthentication);
    assertEquals(authorities, retAuth);
    }

这篇关于如何用通配符模拟泛型方法的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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