Test类要求具体类中的方法是静态的

时间:2016-09-14 08:18:44

标签: java unit-testing junit interface junit4

测试类需要在具体类中将方法定义为static。但是具体类从接口实现了一个方法。

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

接口

public interface ArithmeticSkeleton {

    public int operation(int a, int b);

}

具体类

public class Divide implements ArithmeticSkeleton{

    public int operation(int a, int b) {        
        return (a / b);
    }
}

jUnit测试用例:

public class ArithmeticSkeletontest {

    private ArithmeticSkeleton as;

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

}

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

2 个答案:

答案 0 :(得分:1)

您需要初始化类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
}

答案 1 :(得分:0)

操作方法不是静态的。因此,您必须在测试

中实例化Divide类的对象
@Test
public void testDivision() throws Exception {
    assertEquals("5", new Divide().operation(10, 2));
}
相关问题