Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JMockit:如何调用模型的自定义方法_Java_Jmockit - Fatal编程技术网

Java JMockit:如何调用模型的自定义方法

Java JMockit:如何调用模型的自定义方法,java,jmockit,Java,Jmockit,我的应用程序使用fastcanager(基于BufferedReader)读取输入(查看我问题的代码部分) 如果我想在测试中替换输入,我应该模拟fastcanaver(查看问题的代码部分) 问题:对于每个输入,我应该制作单独的模型。如果我可以在单个模型内切换输入,那就太好了 问题:如何将自定义方法添加到JMockit实体模型中,然后调用它们?(查看切换输入快速扫描仪的方法实体模型) 代码:[此部分是可选的,仅用于更好地理解] 快速扫描仪 快速扫描仪模型: 新模型(){ 私有int[]input1

我的应用程序使用
fastcanager
(基于
BufferedReader
)读取输入(查看我问题的代码部分)

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

问题:
对于每个输入,我应该制作单独的模型。
如果我可以在单个模型内切换输入,那就太好了

问题:
如何将自定义方法添加到JMockit实体模型中,然后调用它们?
(查看
切换输入
快速扫描仪的方法
实体模型)

代码:[此部分是可选的,仅用于更好地理解]

快速扫描仪

快速扫描仪模型:

新模型(){
私有int[]input1=新int[]{17,2,3,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1};
私有int[]input2=新int[]{5,2,2,3,3,3,3};
私有int[]input3=新int[]{8,2,5,1,3,1,1,1,1,3,1};
专用字节切换器=1;
专用字节指针=0;
//这里的问题是:我如何调用这个方法
public void switchInput(){
切换程序++;
指针=0;
}
@抑制警告(“未使用”)
int nextInt()引发IOException{
int[]输入=null;
开关(扳动器){
案例1:输入=输入1;中断;
案例2:输入=输入2;中断;
案例3:输入=输入3;中断;
}
返回输入[pointer++];
}
};

同一个
实体模型
子类不能同时有多个实例(每个这样的实体模型在实例化时都会覆盖上一个实例)。相反,将
调用
参数添加到
@Mock
方法中,然后使用它提供的信息来区分多个数据集。例如:

@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();
}
@测试
public void testClientWithVariedDataFromFastScanners()
{
新模型(){
//测试数据的一些数据结构
@嘲弄
int nextInt(调用库存){
int idx=inv.getInvocationIndex();
FastScanner fs=inv.getInvokedInstance();
//使用idx或fs作为查找索引查找下一个值
//进入数据结构:
inti=。。。
返回i;
}
};
client.doSomethingUsingFastScanners();
}

此外,如果您想在模拟子类上调用任何方法(包括
静态
方法),只需将其设置为命名类,而不是匿名类。

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

    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;
        }
    }
新模型(){
私有int[]input1=新int[]{17,2,3,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1};
私有int[]input2=新int[]{5,2,2,3,3,3,3};
私有int[]input3=新int[]{8,2,5,1,3,1,1,1,1,3,1};
私有int[]input4=新int[]{4,3,3,2,3,1,1};
私有int[]input5=新int[]{1,1,1,2};
私有int[]input6=新int[]{11,4,2,2,2,0,7,3,2,2,2,4,9,1,4};
@抑制警告(“未使用”)
@嘲弄
int nextInt()引发IOException{
int[]输入=null;
开关(InputCounter.inputNumber){//这里的整点!
案例1:输入=输入1;中断;
案例2:输入=输入2;中断;
案例3:输入=输入3;中断;
案例4:输入=输入4;中断;
案例5:输入=输入5;中断;
案例6:输入=输入6;中断;
}
返回输入[InputCounter.pointer++];
}
};
专用静态类输入计数器{
公共静态字节inputNumber=1;
公共静态字节指针=0;
公共输入(整数){
这个.inputNumber=i;
this.pointer=0;
}
}

您试过了吗?结果如何?@Andy我怎么能试试?我不能使
switchInput
static,它不属于
fastcanaver
API。所以Java在编译时看不到该方法哦,您想从模拟外部调用
switchInput()
。嗯,你为什么要这么做?为什么不创建3个不同的模拟呢?@Andy假设我想测试大约100个输入。我应该创建100个实体模型。一百个模型是个坏主意idea@Andy可能有一些黑客行为,也有其他可能?1)调用
的问题是,在编译时,我不知道什么时候切换输入(在这之后,调用的
nextInt
数量)。2) 我已经编写了
MockUp scannerMockUp=new MockUp()
,但我仍然无法调用自定义方法!错误在哪里?错误在于您仍然在使用一个匿名的模型类。相反,创建一个命名的模型类(即,一个扩展
MockUp
并具有名称的常规类)。
@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();
}
    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;
        }
    }