Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在存根方法时获取InvalidUseOfMatcherException_Java_Tdd_Testng_Mockito - Fatal编程技术网

Java 在存根方法时获取InvalidUseOfMatcherException

Java 在存根方法时获取InvalidUseOfMatcherException,java,tdd,testng,mockito,Java,Tdd,Testng,Mockito,我有以下TestNG测试方法代码: @InjectMocks private FilmeService filmeService = new FilmeServiceImpl(); @Mock private FilmeDAO filmeDao; @BeforeMethod(alwaysRun=true) public void injectDao() { MockitoAnnotations.initMocks(this); } //... another tests here

我有以下TestNG测试方法代码:

@InjectMocks
private FilmeService filmeService = new FilmeServiceImpl();

@Mock
private FilmeDAO filmeDao;

@BeforeMethod(alwaysRun=true)
public void injectDao() {
    MockitoAnnotations.initMocks(this);
}

//... another tests here

@Test
public void getRandomEnqueteFilmes() {
    @SuppressWarnings("unchecked")
    List<Filme> listaFilmes = mock(List.class);

    when(listaFilmes.get(anyInt())).thenReturn(any(Filme.class));
    when(filmeDao.listAll()).thenReturn(listaFilmes);

    List<Filme> filmes = filmeService.getRandomEnqueteFilmes();

    assertNotNull(filmes, "Lista de filmes retornou vazia");
    assertEquals(filmes.size(), 2, "Lista não retornou com 2 filmes");
}
@InjectMocks
private FilmService FilmService=新的FilmService impl();
@嘲弄
私人拍片道拍片道;
@BeforeMethod(alwaysRun=true)
公共空间(DAO){
initMocks(this);
}
//... 这里还有一个测试
@试验
public void getRandomEnqueteFilmes(){
@抑制警告(“未选中”)
List listaFilmes=mock(List.class);
when(listaFilmes.get(anyInt())。然后return(any(Filme.class));
when(filmeDao.listAll()),然后return(listaFilmes);
List filmes=filmeseservice.getRandomEnqueteFilmes();
assertNotNull(filmes,“Lista de filmes Returnou vazia”);
assertEquals(filmes.size(),2,“Lista não Retronou com 2电影”);
}
我得到一个“org.mockito.exceptions.misusing.invalidUseofMatcherException: 参数匹配器的使用无效! 需要0个匹配器,1个记录:“在此代码中调用listAll()方法时:

@Override
public List<Filme> getRandomEnqueteFilmes() {
    int indice1, indice2 = 0;
    List<Filme> filmesExibir = new ArrayList<Filme>();
    List<Filme> filmes = dao.listAll();

    Random randomGenerator = new Random();
    indice1 = randomGenerator.nextInt(5);
    do {
        indice2 = randomGenerator.nextInt(5);
    } while(indice1 == indice2);

    filmesExibir.add(filmes.get(indice1));
    filmesExibir.add(filmes.get(indice2));

    return filmesExibir;
}
@覆盖
公共列表getRandomEnqueteFilmes(){
int指标1,指标2=0;
List filmesExibir=new ArrayList();
List filmes=dao.listAll();
Random randomGenerator=新的Random();
指标1=随机发生器。下一个(5);
做{
指标2=随机发生器。下一个(5);
}而(指标1==指标2);
filmesExibir.add(filmes.get(indicate1));
filmesExibir.add(filmes.get(indicate2));
返膜西比尔;
}
我很确定我错过了什么,但我不知道是什么!有人帮忙吗

when(listaFilmes.get(anyInt())).thenReturn(any(Filme.class));
这是你的问题。不能在返回值中使用
any
any
是一个匹配器,用于匹配存根和验证的参数值,在定义调用的返回值时没有意义。您需要显式返回Filme实例,或者将其保留为null(这是默认行为,这将破坏stubing点)

我应该注意到,使用真实列表而不是模拟列表通常是个好主意。与您开发的自定义代码不同,列表实现是经过良好定义和测试的,并且与模拟列表不同,如果您重构测试中的系统以调用不同的方法,则真正的列表不太可能中断。这是一个风格和测试哲学的问题,但您可能会发现在这里使用真正的列表是有利的


为什么上述规则会导致这种例外?这个解释打破了Mockito的一些抽象,但是-它们将一个值记录到ArgumentMatcher对象的秘密ThreadLocal堆栈上,并返回
null
或其他一些伪值,在调用
的过程中,当
验证
Mockito看到一个非空堆栈,并且知道优先使用那些匹配器而不是实际的参数值。就Mockito和Java求值顺序而言,您的代码如下所示:

when(listaFilmes.get(anyInt())).thenReturn(null);
when(filmeDao.listAll(any())).thenReturn(listaFilmes); // nonsense
自然地,Mockito会看到一个
any
匹配器,并且
listAll
不接受参数,因此需要0个匹配器,记录1个

这是你的问题。不能在返回值中使用
any
any
是一个匹配器,用于匹配存根和验证的参数值,在定义调用的返回值时没有意义。您需要显式返回Filme实例,或者将其保留为null(这是默认行为,这将破坏stubing点)

我应该注意到,使用真实列表而不是模拟列表通常是个好主意。与您开发的自定义代码不同,列表实现是经过良好定义和测试的,并且与模拟列表不同,如果您重构测试中的系统以调用不同的方法,则真正的列表不太可能中断。这是一个风格和测试哲学的问题,但您可能会发现在这里使用真正的列表是有利的


为什么上述规则会导致这种例外?这个解释打破了Mockito的一些抽象,但是-它们将一个值记录到ArgumentMatcher对象的秘密ThreadLocal堆栈上,并返回
null
或其他一些伪值,在调用
的过程中,当
验证
Mockito看到一个非空堆栈,并且知道优先使用那些匹配器而不是实际的参数值。就Mockito和Java求值顺序而言,您的代码如下所示:

when(listaFilmes.get(anyInt())).thenReturn(null);
when(filmeDao.listAll(any())).thenReturn(listaFilmes); // nonsense
自然地,Mockito会看到一个
any
匹配器,并且
listAll
不接受参数,因此需要0个匹配器,记录1个

这是你的问题。不能在返回值中使用
any
any
是一个匹配器,用于匹配存根和验证的参数值,在定义调用的返回值时没有意义。您需要显式返回Filme实例,或者将其保留为null(这是默认行为,这将破坏stubing点)

我应该注意到,使用真实列表而不是模拟列表通常是个好主意。与您开发的自定义代码不同,列表实现是经过良好定义和测试的,并且与模拟列表不同,如果您重构测试中的系统以调用不同的方法,则真正的列表不太可能中断。这是一个风格和测试哲学的问题,但您可能会发现在这里使用真正的列表是有利的


为什么上述规则会导致这种例外?这个解释打破了Mockito的一些抽象,但是-它们将一个值记录到ArgumentMatcher对象的秘密ThreadLocal堆栈上,并返回
null
或其他一些伪值,在调用
的过程中,当
验证
Mockito看到一个非空堆栈,并且知道优先使用那些匹配器而不是实际的参数值。就Mockito和Java求值顺序而言,您的代码如下所示:

when(listaFilmes.get(anyInt())).thenReturn(null);
when(filmeDao.listAll(any())).thenReturn(listaFilmes); // nonsense
自然地,Mockito会看到一个
any
匹配器,并且
listAll
不接受参数,因此需要0个匹配器,记录1个