首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JMockit:如何调用MockUp的自定义方法

JMockit:如何调用MockUp的自定义方法
EN

Stack Overflow用户
提问于 2014-05-06 10:38:17
回答 2查看 1K关注 0票数 0

我的应用程序使用FastScanner (基于BufferedReader)读取输入(请参阅问题的代码部分)。

如果我想在我的测试中替换输入,我应该模拟FastScanner (请看我问题的代码部分)。

问题: For each input I should make separate mockup.,如果我能在单个锁内切换输入,那就太棒了。

问题: How to add custom methods to JMockit MockUps and than call them? (看FastScanner模型的switchInput方法)

代码:本节是可选的,只为您更好地理解

FastScanner

代码语言:javascript
复制
protected static class FastScanner {
    BufferedReader br;
    StringTokenizer st;

    FastScanner(InputStream f) {
        br = new BufferedReader(new InputStreamReader(f));
    }

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
} 

FastScanner MockUp:

代码语言:javascript
复制
new MockUp<FastScanner>() {

        private int[] input1 = new int[] {17, 2, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1};
        private int[] input2 = new int[] {5, 2, 2, 3, 3, 3, 3, 3};
        private int[] input3 = new int[] {8, 2, 5, 1, 3, 1, 1, 1, 1, 3, 1};
        private byte toggler = 1;
        private byte pointer = 0;

        //HERE THE QUESTION: HOW CAN I CALL THIS METHOD
        public void switchInput() { 
            toggler++;
            pointer = 0;
        }

        @SuppressWarnings("unused")
        int nextInt() throws IOException {
            int[] input = null;
            switch (toggler) {
                case 1: input = input1; break;
                case 2: input = input2; break;
                case 3: input = input3; break;
            }
            return input[pointer++];
        }
    };
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-06 15:20:31

您不能同时拥有同一个MockUp子类的多个实例(每个这样的模拟都只是在实例化前一个子类时覆盖它)。相反,向Invocation方法中添加一个@Mock参数,然后使用它提供的信息来区分多个数据集。例如:

代码语言:javascript
复制
@Test
public void testClientWithVariedDataFromFastScanners()
{
    new MockUp<FastScanner>() {
        // some data structure for test data

        @Mock
        int nextInt(Invocation inv) {
            int idx = inv.getInvocationIndex();
            FastScanner fs = inv.getInvokedInstance();

            // Find the next value by using idx or fs as a lookup index
            // into the data structures:
            int i = ...

            return i;
        }
    };

    client.doSomethingUsingFastScanners();
}

另外,如果您想在一个模拟子类上调用任何方法(包括static方法),只需将它变成一个命名类,而不是一个匿名类。

票数 1
EN

Stack Overflow用户

发布于 2014-05-07 07:35:55

也许它比@Rogerio的解决方案更糟糕,但是我们可以使用带有静态字段的外部类。我是说:

代码语言:javascript
复制
    new MockUp<PrisonTransfer.FastScanner>() {

        private int[] input1 = new int[] {17, 2, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1};
        private int[] input2 = new int[] {5, 2, 2, 3, 3, 3, 3, 3};
        private int[] input3 = new int[] {8, 2, 5, 1, 3, 1, 1, 1, 1, 3, 1};
        private int[] input4 = new int[] {4, 3, 3, 2, 3, 1, 1};
        private int[] input5 = new int[] {1, 1, 1, 2};
        private int[] input6 = new int[] {11, 4, 2, 2, 2, 0, 7, 3, 2, 2, 4, 9, 1, 4};

        @SuppressWarnings("unused")
        @Mock
        int nextInt() throws IOException {
            int[] input = null;
            switch (InputCounter.inputNumber) { //THE WHOLE POINT HERE!
                case 1: input = input1; break;
                case 2: input = input2; break;
                case 3: input = input3; break;
                case 4: input = input4; break;
                case 5: input = input5; break;
                case 6: input = input6; break;
            }
            return input[InputCounter.pointer++];
        }
    };

    private static class InputCounter {
        public static byte inputNumber = 1;
        public static byte pointer = 0;

        public void switchInput(int number) {
            this.inputNumber = i;
            this.pointer = 0;
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23492370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档