简体   繁体   中英

Testing with jUnit

I have a class called Menu.java which is used as the interface for my program.

In this Menu.java class, I have a switch/case block which acts as my menu Options.

Basically, I want to use jUnit to test the output of each case in that switch/case block, but I'm struggling to find the best way to do it.

Is it best to have a seperate jUnit TestCase for each menu operation? and then use a single TestUnit to run all cases? Or is there a better way this can be done?

Many thanks.

In general, each class has a corresponding test class. So you would have a MenuTest.java to match your Menu.java. This allows you to find quickly the tests that are associated with a particular file, because of the naming convention.

Then, ideally, each test would have one test method associated with it. So if your switch has 10 cases, you would end up with 10 test methods, one for each case. This allows you to isolate quickly the option that is failing, because you get feedback for each test individually.

Note that TestCase is JUnit 3. If possible, use JUnit 4 tests (org.junit.*), these are annotated with a @Test annotation.

I would use a single test case for each possible work flow. Ideally you would have one assertion per test case, which is probably easier following that guideline. In general you want to keep your unit tests small and concise.

Then I would place all the test cases in the same test class as long as they belong to the same tested class.

I'd create a single class for Menu.java (MenuTest.java). I'd write a test case for each menu option. If you have GUI stuff, separate it from the logic.

No need to test the GUI or its plumbing.

If the method you are testing is receiving different parameters for this switch, consider using a parameterized test case. The advantage is that it's easier to keep track of what need to be changed if your switch changes:

Here is how it works with TestNG (look up "parameterized test case" for JUnit)

// This method will provide data to any test method that declares that
// its Data Provider is named "test1"
@DataProvider(name = "test1")
public Object[][] createData1() {
 return new Object[][] {
   { "Foo", new Integer(36) },
   { "Bar", new Integer(37)},
 };
}

// This test method declares that its data should be supplied by the
// Data Provider named "test1"
@Test(dataProvider = "test1")
public void verifyData1(String n1, Integer n2) {
 System.out.println(n1 + " " + n2);
}

In your case I'd use single test case for all options. You need something like this:

import org.junit.Test;
import org.junit.Assert;
import your.project.Menu;

public class MenuTest {
@Test
public void testCase() {
Menu menu = new Menu();
assertEquals("1",menu.runCase("bar"));
assertEquals("2",menu.runCase("foo"));
//etc
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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