0

I have never used JUnit before and I'm having some trouble setting up the tests. I have a Java project and a package, both named 'Project1' with one class which I'm trying to test called 'Module'. 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.

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

}

4 Answers4

1

Make your Module variable an instance variable in your test class, instead of a local variable in a method. Then the @Before method will just initialize the variable, not declare it too. Then it will be in scope in any @Test method.

Incidentally, compare your string contents with String's equals method, not ==.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • actually you can omit the setUp method here and initialize the mebmber variable directly. Junit will create a new instance of your test class for each test case before running it. – Puce Mar 27 '14 at 17:56
  • @Puce Related: [Best Practice: Initialize JUnit class fields in setUp() or at declaration?](http://stackoverflow.com/a/526085/1426891) – Jeff Bowman Mar 27 '14 at 18:02
0

Always use equals:

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

furthermore declare csc8001 as a class member.

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

    }
nano_nano
  • 12,351
  • 8
  • 55
  • 83
0

Make your Module an instance variable. 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. 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.

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.

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

See assertEquals and more at the org.junit.Assert documentation.

p.s. 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.

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
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()) ;
    }

}
Puce
  • 37,247
  • 13
  • 80
  • 152