javaunit-testingjunit4method-group

How to write Unit test for a group of methods in which each method depends on others?


I'm new in writing Unit test & I got stuck while writing test cases for below scenario:

 Class A
 {
     public B createB(string name);
     public B getB(string name);
     public void removeB(B instance);
 }

 Class B
 {
     public B(string name);
 }

The method createB() will return null if an instance of B which the specific name has already existed. So, to test the case createB() completed successfully, I've to call getB() to check if it exists then removeB() to remove it:

 A a = getInstanceOfA();
 public void testCreateB()
 {
     B b = a.getB("B");
     if (b != null)
     {
         a.removeB(b);
     }
     assertNotNull(a.createB("B"));
 }

This is violating the rule of "a test case must fail for only one reason", if the test is fail, I can't know where is the problem come from. It's the same for testing the getB() method, createB() has to be called as a prerequisite to start the test. I'm using JUnit, the test cases are shuffled each time I start the test. Is there any best practice to ensure the isolation of test cases in this scenario?

One more notice, the getInstanceOfA() method is an API call provided from another library, and it's not controllable to get a 'clean' instance of A.


Solution

  • You need to put your program on a know state for the string "B", call createB method with "B" and check the result is what you expect. Then you do that again, with a different state and a different expected result. Like this:

    public void testCreateBSucess() {
        A a = getInstanceOfA(); // Make sure "B" doesn't exists
        B b = a.createB("B");
        assertNotNull(b);
    }
    
    public void testCreateBFail() {
        A a = getInstanceOfA();
        a.createB("B"); // Making sure "B" exists
        B b = a.createB("B");
        assertNull(b);
    }