2014-03-27 214 views
0

我从未使用过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"); 
     } 
    } 

} 

回答

0
import static org.junit.Assert.assertEquals; 

public class TestCase { 

    private final Module csc8001 = new Module("CSC8001", "Programming and data structures", 20, 0, 0, 7); 


    @Test 
    public void testGetCode() { 
     assertEquals("Some error message", "CSC8001", csc8001.getCode()) ; 
    } 

} 
1

使您的Module变量成为您的测试类中的实例变量,而不是方法中的局部变量。然后@Before方法将刚刚初始化该变量,而不是声明它。那么它将在任何@Test方法的范围内。

顺便提一句,compare your string contents with String's equals method, not ==

+0

实际上,您可以在此省略setUp方法并直接初始化mebmber变量。在运行之前,Junit会为每个测试用例创建一个新的测试类实例。 – Puce

+0

@Puce相关:[最佳实践:初始化setUp()或声明中的JUnit类字段?](http://stackoverflow.com/a/526085/1426891) –

0

始终使用等于:

if (csc8001.getCode().equals("CSC8001")) { 

此外声明csc8001作为一个类的成员。

public class TestCase { 
private Module csc8001; 

@Before 
    public void setUp() throws Exception { 
     csc8001 = new Module("CSC8001", "Programming and data structures", 20, 0, 0, 7); 

    } 
0

让你Module一个实例变量。请记住,对于每个单独的@Test方法,JUnit将创建一个单独的测试类实例,并在其上运行所有的方法@Before。尽管你可以在你声明的同一个地方实例化你的系统,it may be advantageous to keep it in @Before as you have it

public class TestCase { 
    private Module csc8001; 

    @Before public void setUp() throws Exception { 
    csc8001 = new Module("CSC8001", "Programming and data structures", 20, 0, 0, 7); 
    } 

    @Test public void test() { /* ... */ } 
} 

您还可以使用assertEquals检查平等,这将自动fail有一个明确的信息,如果该参数不匹配。

@Test 
public void codeShouldEqualCSC8001() { 
    assertEquals("CSC8001", csc8001.getCode()); 
} 

assertEquals多在org.junit.Assert documentation

p.s.请记住,前缀test和名字setUp从JUnit 3中遗留下来的,而且使用JUnit4或更好(与像@Before@Test注解),您可以自由添加多个@Before@After方法,给你所有的方法无约束的名字。