1. <i id='3JweL'><tr id='3JweL'><dt id='3JweL'><q id='3JweL'><span id='3JweL'><b id='3JweL'><form id='3JweL'><ins id='3JweL'></ins><ul id='3JweL'></ul><sub id='3JweL'></sub></form><legend id='3JweL'></legend><bdo id='3JweL'><pre id='3JweL'><center id='3JweL'></center></pre></bdo></b><th id='3JweL'></th></span></q></dt></tr></i><div id='3JweL'><tfoot id='3JweL'></tfoot><dl id='3JweL'><fieldset id='3JweL'></fieldset></dl></div>
    2. <tfoot id='3JweL'></tfoot>
        <bdo id='3JweL'></bdo><ul id='3JweL'></ul>
      <legend id='3JweL'><style id='3JweL'><dir id='3JweL'><q id='3JweL'></q></dir></style></legend>

        <small id='3JweL'></small><noframes id='3JweL'>

        Mockito 监视正在单元测试的对象

        时间:2023-09-25
      1. <i id='H0XOk'><tr id='H0XOk'><dt id='H0XOk'><q id='H0XOk'><span id='H0XOk'><b id='H0XOk'><form id='H0XOk'><ins id='H0XOk'></ins><ul id='H0XOk'></ul><sub id='H0XOk'></sub></form><legend id='H0XOk'></legend><bdo id='H0XOk'><pre id='H0XOk'><center id='H0XOk'></center></pre></bdo></b><th id='H0XOk'></th></span></q></dt></tr></i><div id='H0XOk'><tfoot id='H0XOk'></tfoot><dl id='H0XOk'><fieldset id='H0XOk'></fieldset></dl></div>

              <tbody id='H0XOk'></tbody>
              <legend id='H0XOk'><style id='H0XOk'><dir id='H0XOk'><q id='H0XOk'></q></dir></style></legend>
              <tfoot id='H0XOk'></tfoot>
            • <small id='H0XOk'></small><noframes id='H0XOk'>

                  <bdo id='H0XOk'></bdo><ul id='H0XOk'></ul>
                  本文介绍了Mockito 监视正在单元测试的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  监视正在单元测试的对象是否是代码异味?例如,假设我有一个 LineCounter 类,其工作是简单地计算字符串中的行数.--

                  Is it a code smell to spy on an object that is being unit tested? For example say I have a LineCounter class whose job is to simply count the number of lines in a string. --

                  class LineCounter {
                      public int getNumLines(String string) {
                          String metadata = getStringMetadata(string);
                  
                          // count lines in file
                          return numLines;
                      }
                  
                      /** Expensive operation */
                      protected String getStringMetadata(String string) {
                          // do stuff with string
                      }
                  }
                  

                  现在我想为此编写一个 JUnit 4 测试来测试 getNumLines 方法,同时模拟昂贵的 getStringMetadata 调用.我决定使用 Mockito 的间谍机制让 getStringMetadata 返回一个虚拟值.

                  Now I want to write a JUnit 4 test for this to test the getNumLines method while mocking out the expensive getStringMetadata call. I decide to use Mockito's spy mechanism to have getStringMetadata return a dummy value.

                  class LineCounterTests {
                      @Test public void testGetNumLines() {
                          LineCounter lineCounterSpy = Mockito.spy(new LineCounter());
                  
                          // Mock out expensive call to return dummy value.            
                          Mockito.when(lineCounterSpy.getStringMetadata(Mockito.anyString()).thenReturn("foo");
                  
                          assertEquals(2, lineCounterSpy.getNumLines("hello
                  world");
                      }
                  }
                  

                  这是合理的做法吗?我觉得测试一个 Spy 对象而不是实际的类很奇怪,但我真的想不出反对它的理由.

                  Is this a reasonable thing to do? I feel pretty weird testing a Spy object rather than the actual class, but I can't really think of a reason against it.

                  推荐答案

                  我将分两部分回答这个问题.首先,是的,模拟或监视被测类是代码异味.这并不意味着它不能正确完成,而是它容易发生风险,应尽可能避免.

                  I will answer the question in two parts. First, yes it is code smell to mock or spy the class under test. That does not mean that it cannot be done correctly but that it is risk prone and should be avoided whenever possible.

                  WRT 你的具体例子,我会看看如何正确使用间谍,但这将取决于你在其他地方已经完全单元测试的断言 getStringMetadata.这就引出了一个问题,如果您在其他地方对 getStringMetadata 进行了完整的单元测试,那么您必须知道如何测试它,因此为什么不测试 getNumLines 没有间谍.

                  WRT your specific example, I would see how the spy could be correctly used but that would be predicated on the assertion that you have elsewhere fully unit tested getStringMetadata. This then begs the question, if you have fully unit tested getStringMetadata elsewhere then you must know how to test it and therefore why not test getNumLines without the spy.

                  话虽如此,millhouse 提出了一个很好的观点,但无论哪种方式,您都必须在某处对昂贵的代码进行单元测试.他的建议有助于隔离昂贵的代码并确保您只需测试/练习一次.

                  All this being said, millhouse makes a good point but either way you have to unit test the expensive code somewhere. His suggestion goes a long way to help isolate the expensive code and ensure that you only have to test / exercise it once.

                  这篇关于Mockito 监视正在单元测试的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Mockito 模拟方法行为时以任何顺序匹配列表 下一篇:java.lang.LinkageError: ClassCastException

                  相关文章

                  最新文章

                  <small id='Xv7Kn'></small><noframes id='Xv7Kn'>

                    <tfoot id='Xv7Kn'></tfoot>
                  1. <i id='Xv7Kn'><tr id='Xv7Kn'><dt id='Xv7Kn'><q id='Xv7Kn'><span id='Xv7Kn'><b id='Xv7Kn'><form id='Xv7Kn'><ins id='Xv7Kn'></ins><ul id='Xv7Kn'></ul><sub id='Xv7Kn'></sub></form><legend id='Xv7Kn'></legend><bdo id='Xv7Kn'><pre id='Xv7Kn'><center id='Xv7Kn'></center></pre></bdo></b><th id='Xv7Kn'></th></span></q></dt></tr></i><div id='Xv7Kn'><tfoot id='Xv7Kn'></tfoot><dl id='Xv7Kn'><fieldset id='Xv7Kn'></fieldset></dl></div>
                      <bdo id='Xv7Kn'></bdo><ul id='Xv7Kn'></ul>

                    <legend id='Xv7Kn'><style id='Xv7Kn'><dir id='Xv7Kn'><q id='Xv7Kn'></q></dir></style></legend>