简体   繁体   English

JUnit设置测试用例

[英]JUnit set up test case

I have never used JUnit before and I'm having some trouble setting up the tests. 我以前从未使用过JUnit,并且在设置测试时遇到了一些麻烦。 I have a Java project and a package, both named 'Project1' with one class which I'm trying to test called 'Module'. 我有一个Java项目和一个程序包,都命名为“ Project1”,并且有一个我要测试的类称为“模块”。 At the moment I'm just wanting to check if the values are correct. 目前,我只想检查值是否正确。

Module class 模块类别

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

} }

Test case 测试用例

Here is where I get lost. 这是我迷路的地方。 I'm trying to give some dummy values to test but I'm not sure how to pass the instance of Module to test. 我正在尝试提供一些虚拟值进行测试,但不确定如何通过Module实例进行测试。

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");
        }
    }

}

Make your Module variable an instance variable in your test class, instead of a local variable in a method. 使您的Module变量成为测试类中的实例变量,而不是方法中的局部变量。 Then the @Before method will just initialize the variable, not declare it too. 然后@Before方法将只初始化变量,而不也声明它。 Then it will be in scope in any @Test method. 然后它将在任何@Test方法的范围内。

Incidentally, compare your string contents with String 's equals method, not == . 顺便说一句, 将您的字符串内容与Stringequals方法(而非==

Always use equals: 始终使用等于:

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

furthermore declare csc8001 as a class member. 此外,将csc8001声明为类成员。

public class TestCase {
private Module csc8001;

and

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

    }

Make your Module an instance variable. 使您的Module成为实例变量。 Remember that for each separate @Test method, JUnit will create a separate instance of your test class and run all of your @Before methods on it. 请记住,对于每个单独的@Test方法,JUnit将创建您的测试类的单独实例,并在其上运行所有@Before方法。 Though you can instantiate your system under test in the same place you declare it, it may be advantageous to keep it in @Before as you have it . 尽管您可以在声明它的相同位置实例化被测系统, 但将其保留在@Before可能是有利的

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() { /* ... */ }
}

You can also use assertEquals to check equality, which will automatically fail with a clear message if the parameters don't match. 您还可以使用assertEquals来检查相等性,如果参数不匹配,它将自动fail并显示一条清晰消息。

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

See assertEquals and more at the org.junit.Assert documentation . 请参阅org.junit.Assert文档中的 assertEquals和更多内容

ps Remember that the prefix test and the name setUp are holdovers from JUnit 3, and that using JUnit4 or better (with annotations like @Before and @Test ) you are free to add multiple @Before and @After methods and give all your methods unconstrained names. ps请记住,前缀test和名称setUp是JUnit 3的保留,并且使用JUnit4或更高版本(带有@Before@Test类的注释),您可以自由添加多个@Before@After方法,并使所有方法不受约束名称。

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()) ;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM