Question

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.

Était-ce utile?

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

Autres conseils

Have a look at some mocking framework like mockito, this will give some mock responses to other functions controlled by the developer, and you can test just the required function.

  • Try to use JUnit 4 annotations like @Before and @After with your methods.
  • Or just write a complex long test. There are a lot of cases when you cannot write tests without violating a general rules of test writing. It's O.K. to write a very long test if you really need it.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top