簡體   English   中英

無法覆蓋Junit的測試用例?

[英]unable to cover test cases in Junit?

我試圖通過為Stack方法,push(),pop()和peak()編寫單元測試來理解Junit和eclEmma。 但他們都失敗了。 似乎沒有人被覆蓋。 我認為最初這是一個語法問題,我的代碼如何將整數對象推入堆棧但似乎不是問題。

import static org.junit.jupiter.api.Assertions.*;

import org.junit.Before;
import org.junit.jupiter.api.Test;
import java.util.Stack;

public class StackMethodTesting {
    private Stack<Integer> aStackOfInt;

    @Before
    public void initialize()
    {
        aStackOfInt = new Stack<Integer>();
        System.out.println(" a new Stack");
    }

    @Test
    public void testpush() {
        aStackOfInt.push(new Integer(1));
        assertEquals(true,aStackOfInt.peek().equals(new Integer(1)));
    }
    @ Test
    public void testPop() {
        aStackOfInt.push(22);
        assertEquals (new Integer(22),aStackOfInt.pop());
    }
    @Test
    public void testpeek()
    {
        aStackOfInt.push(222);
        assertEquals(new Integer(222),aStackOfInt.peek());
    }


}

我假設突出顯示的紅色代碼表示它們沒有被執行。 如果是這樣,我不知道出了什么問題。 運行結果如下:

在此輸入圖像描述

您在測試JUnit4和JUnit5中混合使用JUnit API。 所以,如果你想使用最新的(我推薦你的JUnit 5),你應該從JUnit5包中導入所有東西:org.junit.jupiter。

所以,你的測試用例看起來像這樣(注意我也做了一些其他的改動):

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Stack;

class StackMethodTesting {
    private Stack<Integer> aStackOfInt;

    @BeforeEach
    void initialize()
    {
        aStackOfInt = new Stack<Integer>();
        System.out.println(" a new Stack");
    }

    @Test
    void testpush() {
        Integer value = new Integer(1);
        aStackOfInt.push(value);
        assertTrue(aStackOfInt.peek().equals(value));
    }

    @Test
    void testPop() {
        Integer value = new Integer(22);
        aStackOfInt.push(value);
        assertEquals(value, aStackOfInt.pop());
    }
    @Test
    void testpeek()
    {
        Integer value = new Integer(222);
        aStackOfInt.push(value);
        assertEquals(value, aStackOfInt.peek());
    }


}

您可以在此處閱讀有關JUnit5的更多信息,請訪問https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM