Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JUnit black-box or white-box testing?

Is JUnit black-box or white-box testing? I think that it is white-box but i am not sure. I am looking for that but I can't find a clear answer. Even a simple discussion about that would be useful.

like image 294
anna Avatar asked Apr 24 '12 18:04

anna


1 Answers

The use of the word "unit" in the name of "JUnit" might indicate that JUnit is only suitable for unit testing, and since unit testing is practically synonymous with white-box testing, your suspicion is in the right direction. However, things are slightly more nuanced.

Unit testing, when done according to the book, is necessarily white-box testing in all cases except when testing units that have no dependencies.

In the case of units that have no dependencies there is no difference between unit testing and integration testing, (there are no dependencies to integrate,) therefore the notion of white-box vs. black-box testing is inapplicable.

In the case of units that do have dependencies, unit testing is necessarily white-box testing because in order to test a unit and only that unit you are not allowed to test it in integration with its dependencies, so you have to strip all of its dependencies away, and replace them with mocks, but in doing so you are claiming to know not only what the dependencies of your unit are, but more importantly, in precisely what ways it interacts with them. (Which methods it calls, with what parameters, etc.).

However, despite "unit" being part of its name, JUnit by itself is not limiting you to Unit testing, so it does not impose either a white-box nor a black-box approach. You can use JUnit to do any kind of testing that you like.

It is the addition of a mocking framework such as JMock, Mockito, etc that would make your tests necessarily of the white-box kind.

When I use JUnit I only do what I call Incremental Integration Testing. This means that first I test all units with no dependencies, then I do integration testing on units whose dependencies have already been tested, and so on until everything has been tested. In some cases I substitute some of the dependencies with special implementations that are geared towards testing (for example, HSQLDB instead of an actual on-disk RDBMS) but I never use mocks. Therefore, I never do unit testing except for the fringe case of units with no dependencies, where, as I have already explained, there is no distinction between unit testing and integration testing. Thus, I never do white-box testing, I only do black-box testing. And I use JUnit for all of that. (Or my own testing platform which is largely compatible with JUnit.)

Most of the industry out there appears to be using JUnit to do extensive unit testing (white-box testing) and also their integration (black-box) testing.

like image 149
Mike Nakis Avatar answered Oct 17 '22 19:10

Mike Nakis