简体   繁体   English

如何检查使用JMockit传递的特定模拟实例?

[英]How can I check a specific mocked instance is passed using JMockit?

I want to test the following code 我想测试以下代码

BufferedReader reader = new BufferedReader(new FileReader(args[0]));

If I write a test like this, it works 如果我写这样的测试,那就行得通

@Mocked FileReader fileReader;
@Mocked BufferedReader bufferedReader;

//...

new NonStrictExpectations() {{
  new FileReader("filename"); times = 1;
  new BufferedReader(withAny(fileReader)); times = 1;
}};

However, this test does not make sure that the create FileReader is passed to the ctor of BufferedReader, only that a FileReader gets passed. 然而,本次测试没有请确保创建的FileReader传递到BufferedReader类的构造函数,只有一个的FileReader被传递。

What I actually want is for the last line to be 我真正想要的是最后一行

new BufferedReader(withSameInstance(fileReader)); times = 1;

Unfortunately this doesn't work, as JMockit complains that the ctor of BufferedReader is never called with the specified argument... 不幸的是,这是行不通的,因为JMockit抱怨BufferedReader的ctor从来没有使用指定的参数来调用...

I tried using @Captured on the fileReader but that didn't work either... 我尝试在fileReader上使用@Captured,但是那也不起作用...

The ability that @Capturing mock fields had of getting new -ed instances assigned to them was removed in JMockit 1.6, in an attempt to simplify the API. 为了简化API,在JMockit 1.6中删除了@Capturing模拟字段获取分配给它们的new -ed实例的功能。

With the current API (JMockit 1.6 & 1.7) you can still achieve the desired effect, in one of two ways: 使用当前的API(JMockit 1.6和1.7),您仍然可以通过以下两种方式之一实现所需的效果:

@Mocked FileReader fileReader;
@Mocked BufferedReader bufferedReader;
FileReader capturedReader;

@Test
public void mockIOClasses() throws Exception {
    new NonStrictExpectations() {{
        new FileReader("filename");
        result = new Delegate() {
            void captureIt(Invocation inv) {
                capturedReader = inv.getInvokedInstance();
            }
        };
        times = 1;

        new BufferedReader(with(new Delegate<Reader>() {
            void check(Reader in) { assertSame(capturedReader, in); }
        }));
        times = 1;
    }};

    new BufferedReader(new FileReader("filename"));
}

@Test
public void mockIOClasses2() throws Exception
{
    new NonStrictExpectations() {{
        new FileReader("filename");
        result = new Delegate() {
            void captureIt(Invocation inv) {
                capturedReader = inv.getInvokedInstance();
            }
        };
    }};

    new BufferedReader(new FileReader("filename"));

    new Verifications() {{
        FileReader r;
        new BufferedReader(r = withCapture());
        assertSame(capturedReader, r);
    }};
}

This said, however, I would recommend to avoid mocking the JRE IO classes. 这就是说,但是,我建议避免模拟JRE IO类。 Both tests above are too tightly coupled to implementations details. 上面的两个测试都与实现细节紧密联系在一起。 The best approach is to just use a real file; 最好的方法是只使用一个真实的文件。 you should be able to use a small test file (in the "test" source dir) here, perhaps creating it as a temporary file in the test itself. 您应该可以在此处使用一个小的测试文件(在“ test”源目录中),也许可以在测试本身中将其创建为临时文件。

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

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