Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling methods that are not subject to the test?

Tags:

unit-testing

I'm currently trying to figure out how to write proper unit tests. There still is a lot to learn. One scenario I would like to ask about is the following:

Suppose I am testing method A of class X. Now I often find it necessary to call a method B (of X) that is not subject of the current test. Most of the time, this happens to either set up the test fixture (i.e. changing the state of X) or asserting something about A (i.e. making an assertion about the state of X). In both cases, mocking is futile. However, if I make any (faulty) changes to B, then test A may fail even though nothing about A has changed.

Now, I am unsure about how to interpret this outcome. Does it mean that my testing process or design is flawed, or is it a non-issue since I am, after all, testing the unit "X" (the class)?

like image 729
Andy Brandi Avatar asked Jul 20 '15 14:07

Andy Brandi


People also ask

Should I test all the methods?

Yes, you should test them all. At some point in the future the implementation of one of the calling methods, say MethodA , may change and you need to test if all the old uses are still applicable.

Can we test private methods in unit testing?

Why We Shouldn't Test Private Methods. As a rule, the unit tests we write should only check our public methods contracts. Private methods are implementation details that the callers of our public methods aren't aware of. Furthermore, changing our implementation details shouldn't lead us to change our tests.

How do you call a method in JUnit test case?

Create Test Runner Class It imports the JUnitCore class and uses the runClasses() method that takes the test class name as its parameter. Compile the Test case and Test Runner classes using javac. Now run the Test Runner, which will run the test case defined in the provided Test Case class. Verify the output.

Can we test private methods in JUnit?

So whether you are using JUnit or SuiteRunner, you have the same four basic approaches to testing private methods: Don't test private methods. Give the methods package access. Use a nested test class.


1 Answers

If the reason the test of A fails is because the object of class X is in an invalid state as a result of a problem with B, there is not necessarily any problem with your testing design. Really you should have some unit tests of B which fail before you even get to testing A. That way, you can assume that if those tests pass, any test failure of A is a result of a problem with A.

The typical way to write unit tests is to assume that every method not under test works properly. This is only useful, though, if those other methods have their own unit tests to catch bugs when they are introduced. If you don't have that, though, a failure of your test case for A that is caused because you recently introduced a bug into B could leave you barking up the wrong tree.

like image 65
Daniel Avatar answered Oct 23 '22 05:10

Daniel