3

Should I ignore unit tests for new constructors? Should I make a new Test class for the different constructors and just copy the tests? The tests themselves did not change. It would be nice if there was a way to run every test just with different constructors. To visualize what's going on, I added code below.

I had a class that looked like this:

public class Foo {
    public Foo() {...}

    public void doStuff(){...}
    public void doThing(){...}
}

And I have a test class that looked like this:

public class FooTest {
    Foo foo;

    @Before
    public void setUp() {
        foo = new Foo();
    }

    @Test
    public void fooCanDoThing() {
        foo.doThing();
        assertThat(...);
    }

    @Test
    public void fooCanDoStuff() {
        foo.doStuff();
        assertThat(...);
    }
}

I recently had a task that included adding more constructors to my foo class

public class Foo {
    ...
    public Foo(String bleh) {...}
    public Foo(String bleh, String blah) {...}
    ...
}

What is the best way to go about testing with multiple constructors?

Edit: To clear up some things, foo extends thing

public class Foo extends Thing {...}

Lets say that Thing has and handled function of getBlah and I already tested for getBlah. But now, I can actually set my own "blah."

Pickle_Jr
  • 85
  • 8
  • you can easily test them together with other method tests. after all, in order to test any of the methods of the Foo class, you'll first need to run (at least) one constructor. chances are, in one test you'll test the second param, in another, you won't. – Stultuske Feb 11 '19 at 15:00
  • But if you write tests like this: foo = new Foo; I hope you understand they won't work very well. – Stultuske Feb 11 '19 at 15:01

1 Answers1

2

You should be testing behavior of the SUT in your test class.

If that additional constructor param introduces a new behavior to your class then you should create a test for it with a meaningful name:

shouldDoThingXXX_givenBleh()

shouldDoThingYYY_givenBlehAndBlah()

So most likely if you only set one variable, then your current tests will run as before but does the introduction of a an additional one introduces new behavior paths and different possible outcomes of certain functionalities?

If yes, then you should exhaust them with additional clearly named tests.

Summing up adding a new constructor does not mean, the double amount of existing tests, it may but it depends on the introduces / altered behavior of the SUT.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • Ahh, I need to update my question. So this class extends another class. Let's say the extends class has a method `get blah`. Before, blah was set and handled in the parent class. Now, you can set your own "blah." So I already have a test that tests getBlah. – Pickle_Jr Feb 11 '19 at 15:13
  • Again, does the actual behavior change? Regardless of the source of the variables? – Maciej Kowalski Feb 11 '19 at 15:21
  • It does not implement any behavior change, just variable setters – Pickle_Jr Feb 11 '19 at 15:45
  • 1
    Then I would not duplicate the test cases. If you want to make sure your constructor does what is supposed to, then create a test something like, shouldInstantiateState_givenBlehAndBlah – Maciej Kowalski Feb 11 '19 at 15:49
  • Thank you so much for the help! For anybody that happens to stumble upon this, if your new constructors add behaviors, then those behaviors should be tested. But if it's just adding variable setters, then just create a test to make sure that the constructor does with it is supposed to. – Pickle_Jr Feb 11 '19 at 15:52