Testing of private methods would depend on their complexity some one line private methods wouldn't really warrant the extra effort of testing (this can also be said of public methods), but some private methods can be just as complex as public methods, and difficult to test through the public interface. Logging also "breaks encapsulation", but we still do it, because it's so useful. It doesn't have to be a unit test on a private method - it could be, for example, logging. The second one can expose more to ensure that the code is adequately tested. There is an interface for the users, and an interface for the maintainers. In general, a class may be offering more than one interface. It means that when you change the implementation, you may need to change your tests, but that's OK - each change of the implementation requires re-testing the code, and if the tests need to be modified to do that, then you just do it. Unit tests are simply part of the package you're developing, and it's OK to have a dependency between the tests and the code which is being tested. In Java, you can simply give the method "default" access and put the unit test in the same package. produce the correct output) than a nice OOP design which doesn't work correctly, because it wasn't tested adequately. It's more important to have the correct code (i.e. When faced with a choice between encapsulation and testability, I'd rather go for the second. You don't want to have "twin classes" which are always changed together. splitting a pricing model into two pieces just to avoid testing private methods is not very intuitive and may lead to problems later on when maintaining/extending the code). On the other hand, sometimes factoring out the implementation details into separate classes leads to classes with complex interfaces, lots of data passing between the old and new class, or to a design which may look good from the OOP point of view, but does not match the intuitions coming from the problem domain (e.g. You should then test the new class and call the public interface of it in your old class. This may encourage you to write it in a way which is reusable. If your private method is so complex that it needs a separate unit test, it often means that it deserved its own class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |