Mockito的when和thenReturn语句在Java中对于2次方法调用不起作用。

huangapple go评论132阅读模式
英文:

Mockito when and thenReturn statement not working for 2 method calls in Java

问题

在我的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},但在第二种情况下,它会给出一个null的引用。

此外,在调试时,有时候会给出这个调试错误,即Partition不能通过toString()返回。

编辑

现有的测试方法是这样的:

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方法。

英文:

Inside my Test class, I have the following mocking statement:

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

Basically, I am calling this above statement through two different test methods. One is existing and works fine, second is my newly written code which calls the same method. It's mentioned inside the setup method. It gets executed in both the cases and when I evaluate the value, it gives an object reference in both the cases, like this:

result= {Assembly$Partition@3793}

The code in my class that it is mocking is:

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

But when the debugger goes from test method to the code, the builder creates an object reference in the first case i.e. granularity= {Assembly$Partition@3892}, but in the second case, it is giving the reference as null.

Also, sometimes while debugging, it gives me this debug error that Partition cannot be returned by toString().

Edit

Existing test method is this:-

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

And my new method is:-

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

Both methods compute various data to call the publishFile method.

答案1

得分: 1

你真的没有添加足够的(真实的)代码来确定这一点,所以不要期望得到一个真正的答案!以下是一个猜测:

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

... 仅当确切的 message 到达时才模拟。publish2 示例是:

filePublisher.publishSecond(date, id, type);

其中 date != message

尝试这样做:

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

You really have not added enough (real) code to pin this down so don't expect a real answer! Here's a guess:

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

... only mocks when that exact message arrives. The publish2 example is

filePublisher.publishSecond(date, id, type);

where date != message.

Try this:

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

huangapple
  • 本文由 发表于 2020年8月24日 16:00:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63556957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定