Mockito when and thenReturn语句不适用于Java中的2个方法调用

Anmol Khanna:

在我的Test类中,我有以下模拟语句:

when(metadata.getGranularity(message)).thenReturn(new Assembly.Partition.Builder.build());

基本上,我通过两种不同的测试方法来调用上述声明。第一个是现有的并且可以正常工作,第二个是我的新编写的代码,它调用相同的方法。在设置方法中有提及。在两种情况下都将执行它,当我评估值时,在两种情况下都将提供对象引用,如下所示:

result= {Assembly$Partition@3793}

我班上正在嘲笑的代码是:

Assembly.Partition granularity = metadata.getGranularity(message);

但是,当调试器从测试方法转到代码时,构建器在第一种情况下创建对象引用,即granularity= {Assembly$Partition@3892},但是在第二种情况下,它将引用赋予空值。

另外,有时在调试时,它会给我这个调试错误,即toString()无法返回Partition。

编辑

现有的测试方法是:

public void publish()
filePublisher.publishFirst(message, event, name);
verify(file publisher, times (1)).publishFile(anyString(), anyList(Mylist.class));

我的新方法是:

public void publish2()
filePublisher.publishSecond(date, id, type);
verify(file publisher, times (1)).publishFile(anyString(), anyList(Mylist.class));

这两种方法都会计算各种数据以调用publishFile方法。

德雷克伯

您确实还没有添加足够的(实际)代码来确定这一点,所以不要指望真正的答案!这是一个猜测:

when(metadata.getGranularity(message))... 

...仅在准确message到达时进行嘲笑这个publish2例子是

filePublisher.publishSecond(date, id, type);

在哪里date != message

试试这个:

when(metadata.getGranularity(any())).thenReturn(new Assembly.Partition.Builder.build());

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Mockito when()。thenReturn()抛出nullpointerExceptions

Mockito when thenreturn返回null

Mockito嘲笑不适用于主要方法java

gradle中的mockito不适用于java项目

Mockito when()调用如何工作?

使用Mockito的when方法的问题

方法Mockito.when()。thenReturn()和Mockito.doreturn()。when()不返回指定的模拟值

Mockito when-thenReturn with SharedPreference值

基于输入参数Mockito.when thenReturn

Mockito when()。thenReturn()无法正常工作

Mockito NoSuchElementException when() findById() get() thenReturn()

Mockito when ... setUp方法中的return子句

Mockito when()。thenReturn不必要地调用该方法

Mockito中的动态thenReturn()语句

Mockito MockedStatic when()“无法解析方法”

Mockito @Mock不使用when()的方法存根

Mockito的when()不存根超类的方法

Mockito:使用when().. then()测试布尔方法

Mockito 'when' with JPA Repository with ignoreCase finder 方法

Java流的Mockito.when()子句

Java泛型-方法不适用于Mockito生成的存根

的Mockito - 当thenReturn

Mockito.when()。thenReturn()不起作用或返回null

SpringBoot Mockito:when..thenReturn 給出異常

Mockito when()。thenReturn()当返回空列表时返回Null

Mockito when ... thenReturn不返回Collection的期望值

Mockito when()... then()NullPointerException

Mockito @InjectMocks不适用于相同类型的字段

Mockito错误不适用于该参数(无效)