Is it possible to do black box tests when I inject dependencies in the class?

This is my case. I have a class in which I inject a service as dependency, an IService. I want to test one method of this class. This method use one of the methods of the service. Now I want to test this method. For that tests, I create a mock of this service and I configure the method that is used by the method to test to return diferent results according to the needs of the test. That's good because I don't need to connect to the real service to can test the method. The problem is that for that, I need to know what method of the service are used by the method to can configure the mocks. So this makes me to know the implementation of the method. White box test. But what happen if the implementantion of the method changes because it uses another method of the service? I would have to change all my tests and I have to know that the implmentation has changed. Apart of the extra work that I have to do to change the test methods (configure the mocks for the new method), I see another problem. One of the reasons to have tests it is that I could modify the implementation of the method, run the tests and see if I brake something. This is good to refactor code or try to improve performance, for example. Also, this wouldn't allow share the work, for example, one do implementation and another do the tests, because the test couldn't be implemented until the methods to test are finished. So in sumary, I would like to know if I am missing something about tests, or if there is some way to can do black box tests when a depdency is injected in the class, I mean, avoid the need to change the test methods if the implementation of the method changes. Thanks.

May 8, 2025 - 17:51
 0

This is my case.

I have a class in which I inject a service as dependency, an IService.

I want to test one method of this class. This method use one of the methods of the service.

Now I want to test this method. For that tests, I create a mock of this service and I configure the method that is used by the method to test to return diferent results according to the needs of the test. That's good because I don't need to connect to the real service to can test the method.

The problem is that for that, I need to know what method of the service are used by the method to can configure the mocks. So this makes me to know the implementation of the method. White box test.

But what happen if the implementantion of the method changes because it uses another method of the service? I would have to change all my tests and I have to know that the implmentation has changed.

Apart of the extra work that I have to do to change the test methods (configure the mocks for the new method), I see another problem.

One of the reasons to have tests it is that I could modify the implementation of the method, run the tests and see if I brake something. This is good to refactor code or try to improve performance, for example. Also, this wouldn't allow share the work, for example, one do implementation and another do the tests, because the test couldn't be implemented until the methods to test are finished.

So in sumary, I would like to know if I am missing something about tests, or if there is some way to can do black box tests when a depdency is injected in the class, I mean, avoid the need to change the test methods if the implementation of the method changes.

Thanks.