Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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 Junit测试堆栈pop_Java_Junit_Stack - Fatal编程技术网

Java Junit测试堆栈pop

Java Junit测试堆栈pop,java,junit,stack,Java,Junit,Stack,如果我的堆栈工作正常,我将尝试使用JUnitIT测试。我得到了输出: testPopEmptyStack(StackTesting.TestJunitStack): null false 然而,我希望得到一个输出true,因为在我的堆栈类中。如果pop() 堆栈类: public class Stack { Node top; int count = 0; ArrayList<Node> stack = new ArrayList<Node>()

如果我的堆栈工作正常,我将尝试使用JUnitIT测试。我得到了输出:

testPopEmptyStack(StackTesting.TestJunitStack): null
false
然而,我希望得到一个输出
true
,因为在我的堆栈类中。如果
pop()

堆栈类:

public class Stack {
    Node top;
    int count = 0;
    ArrayList<Node> stack = new ArrayList<Node>();

    public boolean checkEmpty() {
        if (count == 0) {
            return false;
        }
        else {
            return true;
        }
    }

    public Node getTop() {
        if (count > 0) {
            return top;
        }
        else {
            return null;
        }
    }

    public void pop() {
        if (count > 0) {
            stack.remove(0);
            count--;
        }
        else {
            throw new EmptyStackException();
        }
    }

    public void push(int data) {
        Node node = new Node(data);
        stack.add(node);
        count++;
    }

    public int size() {
        return count;
    }

}
TestRunnerStack.java:

public class TestJunitStack extends TestCase{

    static Stack emptystack = new Stack();

    @Test(expected = EmptyStackException.class)
    public void testPopEmptyStack() {
        emptystack.pop();
    }
}
public class TestRunnerStack {
    public void main(String[] args) {
        Result result = JUnitCore.runClasses(TestJunitStack.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println(result.wasSuccessful());
    }
}
编辑


static
从这里删除
testpoppentystack
从这里删除
static

public static void testPopEmptyStack() {
...

输出是相同的=[尝试我的简化测试@test(预期=EmptyStackException.class)public void testPopEmptyStack(){抛出新的EmptyStackException();}嗯奇怪,它有相同的输出。我喜欢这个测试用例Fyi:我的测试输出是“真”垃圾…是什么原因造成的