Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 测试类要求混凝土类中的方法是静态的_Java_Unit Testing_Junit_Interface_Junit4 - Fatal编程技术网

Java 测试类要求混凝土类中的方法是静态的

Java 测试类要求混凝土类中的方法是静态的,java,unit-testing,junit,interface,junit4,Java,Unit Testing,Junit,Interface,Junit4,测试类要求在具体类中将方法定义为静态。但具体类从接口实现方法 接口不允许实现的方法是静态的 界面: public interface ArithmeticSkeleton { public int operation(int a, int b); } public class ArithmeticSkeletontest { private ArithmeticSkeleton as; @Test public void testDivision() t

测试类要求在具体类中将方法定义为静态。但具体类从接口实现方法

接口不允许实现的方法是静态的

界面:

public interface ArithmeticSkeleton {

    public int operation(int a, int b);

}
public class ArithmeticSkeletontest {

    private ArithmeticSkeleton as;

    @Test
    public void testDivision() throws Exception {
        assertEquals("5", Divide.operation(10, 2));
    }

}
混凝土等级

public class Divide implements ArithmeticSkeleton{

    public int operation(int a, int b) {        
        return (a / b);
    }
}
jUnit测试用例:

public interface ArithmeticSkeleton {

    public int operation(int a, int b);

}
public class ArithmeticSkeletontest {

    private ArithmeticSkeleton as;

    @Test
    public void testDivision() throws Exception {
        assertEquals("5", Divide.operation(10, 2));
    }

}

但是,测试代码不允许访问Divide.operation。

操作方法不是静态的。因此,您必须在测试中像这样实例化Divide类的对象

@Test
public void testDivision() throws Exception {
    assertEquals("5", new Divide().operation(10, 2));
}

您需要初始化类Divide的对象以访问其方法:

   public void testDivision() throws Exception {
            Divide divide = new Divide();
            assertEquals(5, divide.operation(10, 2));
// you need to change "5" to 5 to pass this test
}

您不能以这种方式访问操作方法,如果您想这样做,必须使用静态方法,否则只需使用new Divide()。操作(10,2)就足够了。使用此处的答案,您需要将
“5”
更改为
5
inside
assertEquals
以使此测试通过。