Suppose there are several classes A, B, and C. We test A. The other classes are mocks. Class A has a method within which it uses B and C, and produces a result.

Is it necessary to test that class A interacts with B and C, or to write one test in which the result of executing method A will be checked?

Do such a test in the following case:

class A implements B.OnBarCallback { private B b; public A(B b) { this.b = b; } public void foo() { b.bar(this); } @Override public void onBar() { // do something } } // тест вызова B void testCallB() { B mockB = mock(B.class); A a = new A(mockB); a.foo(); verify(mockB).bar(any(OnBarCallback.class)); } // тест callback void testACallback() { B mockB = mock(B.class); A a = new A(mockB); a.onBar(); // do verify something } 

Is it correct to manually pull the callback method in the test?

    1 answer 1

    If you go completely by the rules, the unit test should test the minimum unit of code. I have such an internal rule - if I suddenly needed to run a unit test, since I don’t understand how it works, it means that it is not a unit test, it is testing a too large piece of code.

    Therefore, if classes B and C are fairly large and not trivial (for example, they read files from disk, download files from the Internet), then it is better to lock them and test them in class A with mocks. And if the B / C methods add two numbers, then I do not see any particular reason for them to get wet.

    Do I need to test that class A interacts with B and C

    But nobody cancels class interaction tests together. Simply, this is already called integration tests. And they are also needed and useful.

    or write one test in which the result of executing method A will be checked?

    But how many tests to write is a philosophical question. But one usually is not enough.

    • And what to do if A is responding to B as a listener or callback? Do a test that will trigger a callback implementation on A? - Alexey Malchenko
    • one
      do mok for class A, so that when he calls the callback, he sets a flag (this could be moka A field). And then a simple test is written that creates the class mo_A and B, registers the mo__A, calls the desired method in B and checks that the count has twitched. If you need to check that Kolbeck has twitched with the correct parameters - mok_A can remember them all. - KoVadim
    • I added a question, please see. - Alexey Malchenko
    • one
      If you have written the foo method only to potest, if the bar method is called, then this is somehow wrong. Check for the presence of the method is not necessary - this is the work of the compiler If in the code the foo method is bigger and actually called - then why not test whether it is called. Indeed, in real code there can be a series of if and the like. It is possible that under some conditions the colbek should not be called - this can also be tested. - KoVadim
    • one
      Kolbek is a method? method. So it can be tested as a method. And it is not necessary for someone to register it. - KoVadim