Harmonization of unit testing with OOD

Today, TDD is all the rage, and an increasing number of software stores are turning into flexible, scrum, etc. I certainly see the benefits of automated testing, but I also see TDD as contrary to some principles of good object-oriented design.

TDD requires you to insert seams in your code that display implementation details through an interface. Injection injection or the injection of co-authors violate the principle of information hiding. If your class uses co-author classes, then building these co-authors should be internal to the class and not displayed through the constructor or interface.

I have not seen any literature on the conflicts between writing testable code and at the same time adhering to the principles of encapsulation, simplicity and information hiding. Have these problems been resolved in any standard way?

+5
source share
6 answers

The methods and classes that you think are implementation details do indeed represent seams that represent the axes along which you can modify and re-arrange components into new constellations.

, , , . SOLID - .

- SOLID, , - .

, .

, . , . , - . - , , , API.

API , , Facade. . : Inject (DI) ""

+10

, , ?

TDD , , .

, , :

class Zoo {
    Animal exhibit;
}

interface Animal {
    void walk();
}

class Dog extends Animal {
    DogFood food;

    Dog(DogFood food) {
        this.food = food;
    }
}

, .

Zoo DogFood, Dog , , Dog .

+2

Mark Seemann . "" - . TDD , . Lego , -. , TDD OO.

, . private " ", " ". - , , .

+2

, TDD , . Java , ( ), . API .

- "" , , " " TDD, "" .

+1

, TDD - .

Injection Dependency TDD, OO- .

- A B, A B. , , , " ".

, , B . , , , , B (, ), , , .

Java . , B, , B. , , A.

, .

TDD . , TDD OO. DI OO, TDD.

+1

factory. , .

For example, if class A has dependencies B and C that are passed through the constructor, B and C will be provided by the factory method. The object creating A will not know about objects B and C.

Dependency nesting structures such as Google Guice and Ninject can be used to automate factory creation.

0
source

All Articles