简体   繁体   English

模拟测试void方法,重写成员变量

[英]mockito test void Method, override member variable

I'm new to unit-testing and mockito but i need to test this class: 我是单元测试和Mockito的新手,但我需要测试此类:

public class XMLHandler {
    private static final String CONFIG_FILE = "path/to/xml";
    private XMLConfiguration config = null;

    public XMLHandler() {
        try {
            config = new XMLConfiguration(CONFIG_FILE);
            config.setValidating(true);
        } catch (ConfigurationException e) {
            LOGGER.log(Level.SEVERE, e );
        }
    }

    public List<ConfigENtries> getEntries() {     
        // do some stuff
        return list;
    }

    @Override
    public void removeEntry(int index) {
        // remove entry
    }
}

I think i have to override the config variable with a mockup but i don't have a setter, so how would i do this? 我认为我必须用模型覆盖config变量,但是我没有设置器,所以我该怎么做? And what about removeEntry? 那么removeEntry呢? How to test a void method? 如何测试void方法?

I hope someone can help me with this 我希望有人可以帮助我

As you are allowed to modify your class, I would suggest the following structure: 由于允许您修改类,因此我建议采用以下结构:

public class XMLHandler {
  private final XMLConfiguration config;

  public XMLHandler(XMLConfiguration config) {
    this.config = config;
  }

  public List<ConfigENtries> getEntries() {     
    // do some stuff
    return list;
  }

  @Override
  public void removeEntry(int index) {
    // remove entry
  }
}

Make sure that XMLConfiguration is an interface , not a concrete implementation. 确保XMLConfiguration接口 ,而不是具体的实现。 Then you can mock the config parameter passed to your constructor. 然后,您可以模拟传递给构造函数的config参数。 ( Note: you can mock non-final concrete class too, but using an interface would be preferred .) 注意:您也可以模拟非最终的具体类,但最好使用接口 。)

You can then test the public methods of XMLHandler and confirm correct behaviour by inspecting the calls made to config and asserting the method responses are correct. 然后,您可以通过检查对config的调用并断言方法响应正确,来测试XMLHandler的公共方法并确认正确的行为。

Void methods can be tested without problem. 无效方法可以毫无问题地进行测试。 You just need some way of determining that the state of your object and the world has been adjusted corrected. 您只需要某种方法来确定对象和世界的状态已调整为正确。 So, perhaps you need to call a getter method at the end of your test to ensure values have changed. 因此,也许您需要在测试结束时调用getter方法,以确保值已更改。 Or verify expected calls were made to your mock object. 或者验证是否对模拟对象进行了预期的调用。

Although I would also prefer modifying the constructor and pass XMLConfiguration like @DuncanJones suggests (or use some other kind of Dependency Injection), you could use mockito to mock XMLConfiguration by using the @InjectMocks annotation in your test: 尽管我也更喜欢像@DuncanJones建议的那样修改构造函数并传递XMLConfiguration(或使用其他类型的依赖注入),但是您可以在测试中使用@InjectMocks批注来使用@InjectMocks来模拟XMLConfiguration

@RunWith(MockitoJUnitRunner.class)
public class XMLHandlerTest {

    @InjectMocks
    XMLHandler handler = new XMLHandler();

    @Mock
    XMLConfiguration config;

    //...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM