简体   繁体   English

更改模仿间谍的字段

[英]Changing fields of a mockito spy

So lets say I have a class 所以说我有一堂课

class JustAClass() {
  Stirng justAField = "nothing";
}

Now I'm testing this class and I put it into a mock 现在,我正在测试该类,并将其放到一个模拟中

JustAClass realClass = newJustACLass();
JustAClass spyClass = Mockito.spy(realClass);

spyClass.justAField = "something"

Question is: What does the realClass.justAField equal now? 问题是: realClass.justAField现在等于什么?

EDIT: In response to @fge This didn't fail. 编辑:响应@fge这没有失败。

    CSVExport spyClass = Mockito.spy(testClass);
    FileOutputStream wFile = Mockito.mock(FileOutputStream.class);

    spyClass.wFile = wFile;

    Mockito.doThrow(IOException.class).when(spyClass).createBlankWorkbook();
    spyClass.export(testEnabledFields);
    Mockito.doThrow(IOException.class).when(wFile).close();
    spyClass.export(testEnabledFields);

So is the wFile in testClass the mock now, or the original? 那么testClass中的wFile是现在的模拟还是原始模拟?

Pulling this from api doc http://docs.mockito.googlecode.com/hg-history/be6d53f62790ac7c9cf07c32485343ce94e1b563/1.9.5/org/mockito/Spy.html 从api文档http://docs.mockito.googlecode.com/hg-history/be6d53f62790ac7c9cf07c32485343ce94e1b563/1.9.5/org/mockito/Spy.html中提取

Mockito does not delegate calls to the passed real instance, instead it actually creates a copy of it. Mockito 不会将调用委派给传递的真实实例,而是实际上创建它的副本。 So if you keep the real instance and interact with it, don't expect the spied to be aware of those interaction and their effect on real instance state. 因此,如果保留真实实例并与之交互,请不要期望间谍知道这些交互及其对真实实例状态的影响。 The corollary is that when an unstubbed method is called on the spy but not on the real instance, you won't see any effects on the real instance 结果是,当在侦探上调用未打桩的方法但在实际实例上未调用该方法时,您将看不到任何对实际实例的影响

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

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