我從未使用過JUnit,並且在設置測試時遇到了一些麻煩。我有一個Java項目和一個包,都命名爲'Project1',我試着測試一個名爲'Module'的類。此刻,我只是想檢查這些值是否正確。JUnit設置測試用例
模塊類
package Project1;
//This class represents a module
public class Module {
public final static int MSC_MODULE_PASS_MARK = 50;
public final static int UG_MODULE_PASS_MARK = 40;
public final static int MSC_MODULE_LEVEL = 7;
public final static int STAGE_3_MODULE_LEVEL = 6;
private String moduleCode;
private String moduleTitle;
private int sem1Credits;
private int sem2Credits;
private int sem3Credits;
private int moduleLevel;
public Module(String code, String title, int sem1, int sem2, int sem3, int level)
{
moduleCode = code;
moduleTitle = title;
sem1Credits = sem1;
sem2Credits = sem2;
sem3Credits = sem3;
moduleLevel = level;
}
//method to return the module code
public String getCode()
{
return moduleCode;
}
//INSERT A BUNCH OF GET METHODS
}
測試用例
這裏是我迷路。我試圖給一些虛擬的值來測試,但我不知道如何通過模塊的實例來測試。
package Project1;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class TestCase {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Before
public void setUp() throws Exception {
Module csc8001 = new Module("CSC8001", "Programming and data structures", 20, 0, 0, 7);
}
@Test
public void test() {
if (csc8001.getCode() == "CSC8001") {
System.out.println("Correct");
}
else{
fail("Not yet implemented");
}
}
}
實際上,您可以在此省略setUp方法並直接初始化mebmber變量。在運行之前,Junit會爲每個測試用例創建一個新的測試類實例。 – Puce
@Puce相關:[最佳實踐:初始化setUp()或聲明中的JUnit類字段?](http://stackoverflow.com/a/526085/1426891) –