0

I am somewhat new to test driven development and am trying to determine whether I have a problem with my approach to unit testing.
I have several unit tests (let's call them group A) that test whether my method's return is as expected.
I also have a unit test "B" whose passing condition is that an IllegalArgumentException is thrown when my method is given invalid input.
The unit tests in group A fail when the method is given invalid input since the method needs valid input to return correctly.
If I catch the exception, unit test "B" will fail, but if I don't catch the exception, the tests in group A will fail.
Is it OK to have unit tests fail in this way, or can I modify the code in some way so that all tests always pass?
Am I doing TDD all wrong?
Here's a notion of my code for more clarity:

public class Example{
    public static String method(String inputString, int value){
        if(badInput){
            throw new IllegalArgumentException();
        }
        //do some things to inputString
        return modifiedInputString;
    }
}

public class ExampleTests{
    @Test
    public void methodReturnsIllegalArgumentExceptionForBadInput(){
        assertThrows(IllegalArgumentException.class, ()->{Example.method(badInput,badValue);})
    }

    //"Group A" tests only pass with valid input. Bad input causes IllegalArgumentException
    @Test
    public void methodReturnsExpectedType(){
        assertTrue(actual == expected);
    }

    @Test
    public void methodReturnsExpectedValue(){
        assertTrue(actual == expected);
    }

    @Test
    public void methodReturnsExpectedStringFormat(){
        assertTrue(actual == expected);
    }
}
2
  • Unit tests tests that if method produces certain output for certain input. So you should have the following tests: pass input A - get output A1, pass input B - get output B1, pass invalid input - throw exception E, pass another invalid input - throw exception E1. So passing invalid input to the tests that expects valid input is not a proper use case.
    – Ivan
    Commented Mar 12, 2018 at 2:49
  • Thank you, that clarifies it for me. I guess my problem is that I was passing either valid or invalid input to ALL tests, regardless of what they were testing. Commented Mar 12, 2018 at 3:04

1 Answer 1

0

As you've correctly noted in the comment the problem is with too broad test setup and too implicit tests.

Tests are much more readable when they are self-contained on the business level. Common test setup should be focused on setting up technical details, but all business setup should be within each test itself.

Example for your case (a conceptual example, must be reworked to match the details of your implementation):

@Test
public void givenWhatever_whenDoingSomething_methodReturnsExpectedType(){
    given(someInputs);

    Type result = executeSut(); //rename executeSut to actual function under test name

    assertTrue(result == expected);
}

This way by just looking at a test a reader knows what scenario is being tested. Common test setup and helper functions such as given abstract away technical details, so the reader does not get distracted at the first inspection. If they are interested, the details are always available - but are usually less important, such can be hidden.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.