首页 \ 问答 \ 缺少方法调用验证(模拟),但有一个?(Missing method call for verify(mock), but there is one?)

缺少方法调用验证(模拟),但有一个?(Missing method call for verify(mock), but there is one?)

介绍

我正在尝试进行一项测试,验证(使用Mockito v1.9.5的verify )在执行传递Foo.deinit()之后调用接口Bar带有签名deinit()的方法,并且我'我遇到错误我真的不明白。

这是我试图运行的FooTest.java

@RunWith(JukitoRunner.class)
public class FooTest {
    @Inject
    private Foo foo;
    @Inject
    private Bar bar;

    public static class TestModule extends JukitoModule {
        @Override
        protected void configureTest() {
            bind(Foo.class).to(FooImpl.class);
            bind(Bar.class).to(BarImpl.class);
            bindSpy(BarImpl.class);
        }
    }

    @Test
    public void testDeinit() {
        foo.init(mock(Baz.class));
        foo.deinit();
        verify(bar).deinit();
    }

    @After
    public void validate() {
        validateMockitoUsage(); //line YY
    }
}

运行此时, testDeinit()失败并出现以下错误:

org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
  -> at redacted.impl.BarImpl.deinit(BarImpl.java:XX)

Example of correct verification:
  verify(mock).doSomething()

Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods. 
Those methods *cannot* be stubbed/verified. 

at redacted.impl.FooTest.validate(FooTest.java:YY)
at org.jukito.InjectedStatement.evaluate(InjectedStatement.java:96)
at org.jukito.InjectedAfterStatements.evaluate(InjectedAfterStatements.java:58)
at org.jukito.jukitoRunner.run(JukitoRunner.java:197)

我无法从中提取很多有用的信息。 看起来好像错误抱怨verify(bar).deinit()可能最后没有.deinit() ,我可以删除该部分并得到相同的错误。 错误消息中提供的示例特别令人沮丧,因为它看起来几乎与我使用verify相同。

细节

这是我的BarImpl.java

public class BarImpl implements Bar {
    private final Qux qux;
    private final Quux quux;

    @Inject
    public BarImpl(final Qux qux, final Quux quux) {
        this.qux = qux;
        this.quux = quux;
    }

    @Override
    private final void init(Baz baz) {
        quux.init(this);
        qux.init();
    }

    @Override
    public final void deinit() {
        qux.deinit();  //line XX
    }
}

我还不清楚qux.deinit()在这里是如何导致失败的。 这是我的FooImpl.java

class FooImpl implements Foo {
    private final Bar bar;

    @Inject
    public FooImpl(final Bar bar) {
        this.bar = bar;
    }

    @Override
    public void init(Baz baz) {
        bar.init(baz);
    }

    @Override
    public void deinit() {
        bar.deinit(); 
    }
}

导致UnfinishedVerificationException的原因是什么?如何解决?

我是Mockito newb所以我很可能错过了一些基本的东西。 如果我能提供更多信息,请告诉我。 很抱歉,如果已经回答了这个问题,我在这里误解了答案。


Introduction

I'm attempting to make a test which verifies (using Mockito v1.9.5's verify) that a method with signature deinit() in an interface Bar is called after execution of a pass-through Foo.deinit(), and I'm hitting an error I really don't understand.

Here's the FooTest.java which I'm attempting to run:

@RunWith(JukitoRunner.class)
public class FooTest {
    @Inject
    private Foo foo;
    @Inject
    private Bar bar;

    public static class TestModule extends JukitoModule {
        @Override
        protected void configureTest() {
            bind(Foo.class).to(FooImpl.class);
            bind(Bar.class).to(BarImpl.class);
            bindSpy(BarImpl.class);
        }
    }

    @Test
    public void testDeinit() {
        foo.init(mock(Baz.class));
        foo.deinit();
        verify(bar).deinit();
    }

    @After
    public void validate() {
        validateMockitoUsage(); //line YY
    }
}

When running this, testDeinit() fails with the following error:

org.mockito.exceptions.misusing.UnfinishedVerificationException:
Missing method call for verify(mock) here:
  -> at redacted.impl.BarImpl.deinit(BarImpl.java:XX)

Example of correct verification:
  verify(mock).doSomething()

Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods. 
Those methods *cannot* be stubbed/verified. 

at redacted.impl.FooTest.validate(FooTest.java:YY)
at org.jukito.InjectedStatement.evaluate(InjectedStatement.java:96)
at org.jukito.InjectedAfterStatements.evaluate(InjectedAfterStatements.java:58)
at org.jukito.jukitoRunner.run(JukitoRunner.java:197)

Which I haven't been able to extract much useful information from. It seems almost as if the error complains that verify(bar).deinit() might as well not have a .deinit() off the end, and I can remove that portion and get an identical error. The example provided in the error message is especially frustrating, as it appears almost identical to my use of verify.

Details

Here's my BarImpl.java

public class BarImpl implements Bar {
    private final Qux qux;
    private final Quux quux;

    @Inject
    public BarImpl(final Qux qux, final Quux quux) {
        this.qux = qux;
        this.quux = quux;
    }

    @Override
    private final void init(Baz baz) {
        quux.init(this);
        qux.init();
    }

    @Override
    public final void deinit() {
        qux.deinit();  //line XX
    }
}

I'm still unclear how the qux.deinit() causes failure here. Here's my FooImpl.java:

class FooImpl implements Foo {
    private final Bar bar;

    @Inject
    public FooImpl(final Bar bar) {
        this.bar = bar;
    }

    @Override
    public void init(Baz baz) {
        bar.init(baz);
    }

    @Override
    public void deinit() {
        bar.deinit(); 
    }
}

Question

What's causing the UnfinishedVerificationException and how can it be fixed?

I'm a Mockito newb so it's very likely I've missed something fundamental. Please let me know if there's more information I can provide. Sorry if this has been answered already and I've misunderstood answers here on SO.


原文:https://stackoverflow.com/questions/40350009
更新时间:2022-05-07 18:05

最满意答案

我的建议是使用JSON切换到更结构化的comm方式。 您可以定义自定义数据名称和类型,并轻松覆盖它们。 看一看:

https://github.com/bblanchon/ArduinoJson

这里是HTTPClient示例中的一些JSON示例:

  DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);

  JsonObject& root = jsonBuffer.parseObject(client);

  if (!root.success()) {
    Serial.println("JSON parsing failed!");
    return false;
  }

  // Here were copy the strings we're interested in
  strcpy(userData->name, root["name"]);
  strcpy(userData->company, root["company"]["name"]);

My suggestion is to use JSON to switch to more structured way of comm. You can define custom data names and types and easily cover them. Take a look it at :

https://github.com/bblanchon/ArduinoJson

Here some JSON example from the HTTPClient example :

  DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);

  JsonObject& root = jsonBuffer.parseObject(client);

  if (!root.success()) {
    Serial.println("JSON parsing failed!");
    return false;
  }

  // Here were copy the strings we're interested in
  strcpy(userData->name, root["name"]);
  strcpy(userData->company, root["company"]["name"]);

相关问答

更多
  • 所以,这里的问题都是时间问题。 您知道软件序列的缓冲区大小有限制(对于任何硬件UART也是如此),这是256字节,波特率为9600位/秒。 由于有一个起始位,8个数据位和一个停止位(假设您在这里使用9600 8N1,因为它是最常见的),您将每隔(1/9600)* 10秒或1.04接收一个数据字节毫秒。 因此,要接收256个字节,它应该需要大约266毫秒。 这意味着在266毫秒之后,您的缓冲区将完全填满,之后您收到的任何内容都将开始删除以前接收的数据。 而且存在问题的关键 - 您正在向ESP发送命令以从服务器 ...
  • 尝试安装Arduino SAMD板(32位ARM Cortex-M0 +)板(使用“工具” - >“板” - >“板卡管理器”菜单。只需在搜索栏中搜索“samd”,选择最新版本和安装) Try to install Arduino SAMD Boards (32-bits ARM Cortex-M0+) board (using "Tools" -> "Board" -> "Boards Manager" menu. Just search for "samd" in the search bar, cho ...
  • 我已经完成了......更改网址: https://taub-prayer-ramym.c9.io/girls I HAVE SUCCEDED.. CHANGE URL TO: https://taub-prayer-ramym.c9.io/girls
  • 听起来你正在尝试运行在Java 1.7上为Java 1.8编译的IDE版本。 将java升级到1.8应该修复它。 Sounds like you are trying to run a version of the IDE that has been compiled for Java 1.8 on Java 1.7. Upgrading java to 1.8 ought to fix it.
  • 它在很大程度上取决于客户(即消费者)哪些字段是必需的,哪些是强制性的。 唯一一个始终需要的是“HTTP / 1.1 200 OK”。 当然,如果您没有发送OK消息,则需要替换该状态代码。 It largely depends on the client (i.e. consumer) which fields are required and which are mandatory. The only one that is always required is "HTTP/1.1 200 OK". Of ...
  • 基本的伪代码大纲是这样的: int PWM; int minute_ctr; loop() { if (minute_ctr > 1000) { minute_ctr = 0; check HTML page for 1/0 } if (page == 1) PWM++; set PWM for LED (analogWrite) change direction when PWM = 0 or 255 else ...
  • ESP8266绝对可以处理SSL。 事实上,Arduino存储库包含一个SSL客户端,作为其库的一部分。 以下是显示SSL执行基本HTTP请求的示例: https : //github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino The ESP8266 absolutely can handle SSL. In fact, the Arduino repository ...
  • 所以我提出的解决方案并不理想。 使用5V < - > 3.3V逻辑电平转换器会产生某种错误。 我的解决方案是将arduino上的TX / RX引脚直接插入ESP-12上的RX / TX引脚。 ESP-12上的RX / TX引脚不能正式支持5V逻辑,但在我看来它们似乎也是这样。 您自己承担使用5V逻辑的风险 。 我目前正在使用Arduino IDE 1.6.5的设置可以在下面的示意图中看到(尽管适用于ESP-12,而不是原理图中的ESP-1)。 要允许对ESP-12进行编程,必须先按SW1并按住SW2按住它。 ...
  • 使用以太网模块时,可能将Arduino配置为服务器。 Arduino必须通过以太网模块进行监听才能响应传入的连接。 那么,如果你已经开始使用GSM模块,你需要做同样的事情。 您需要将其设置为服务器。 不幸的是,这是不可能的。 网络服务提供商不允许入站连接,因为单个公共IP地址被许多订户共享。 无论发生什么,都清楚的是,另一端的设备不能启动连接; GSM模块必须始终先走。 根据您的意图,您应该让Arduino + SIM900定期使用状态更新ping服务器,或者使用keep-alive HTTP 1.1标头连 ...
  • 我的建议是使用JSON切换到更结构化的comm方式。 您可以定义自定义数据名称和类型,并轻松覆盖它们。 看一看: https://github.com/bblanchon/ArduinoJson 这里是HTTPClient示例中的一些JSON示例: DynamicJsonBuffer jsonBuffer(BUFFER_SIZE); JsonObject& root = jsonBuffer.parseObject(client); if (!root.success()) { Se ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)