3

I have read that dependency injection is good for testing, in that a class can be tested without its dependencies, but the question comes to my mind if Class A depends on Class B or C or any class, testing Class A independent of some class is yielding a test result of zero, not a failed or past test.

Class A was created to do something and if it is not fed anything whether using new key word or setting up the extra files in Spring, Class A won't do any work.

About the idea of making code modular, readable and maintainable: so business classes became cleaner, but all we did was shift confusion from dirty Java business classes to convoluted XML files and having to delete interfaces used to inject to our loosened objects.

In short, it seems we have to make edits and changes to a file somewhere,right?

Please feel free to put me in my place if my understanding is lacking, just a little irritated with learning Spring because I see the same amount of work just rearranged.

3
  • What do you mean by "yielding a test result of zero, not a failed or past test"? Commented Sep 27, 2017 at 3:55
  • I have not done unit test before, so I was coming from the thinking that when I write a class it usually needs something else to produce any meaningful result. I thought that if my class needs some data and I don't give it anything from say another class then my test gave no results, I figure if I wrote anything wrong the compiler would kindly let me know. Maybe I have bad design practices. Commented Sep 27, 2017 at 11:17
  • If you are only looking for dependency injection then use plain simple design patterns (or something like Guice). However spring is an ecosystem which provides all sorts of integrated components for minimal configuration. By they we nowadays we favor annotations over xml. Commented Sep 28, 2017 at 23:32

3 Answers 3

2

Dependency injection is good for unit testing because you can individually test each method without that method depending on anything else. That way each unit test can test exactly one method.

I would say that if the xml is what’s annoying you check out Spring boot. It’s based on a java configuration so no xml and it simplifies a lot of configuration for you based on your class path. When I first started spring I found the xml very daunting coming from a java background but the annotation based configuration and the auto configuring done by spring boot is extremely helpful for quickly getting applications working.

Sign up to request clarification or add additional context in comments.

1 Comment

And with Spring Boot you can easly use a mock framework for testing like Mockito. See site.mockito.org
2

IMO biggest advantage of using the spring is dependency injection which makes your life easy. For example if you would like to create a new service with three dependencies, then you can create a class very easily using Spring. But without spring, you will end up writing different factory methods which will return you the instances you are looking for. This makes your code very verbose with static method calls. You may want to take a look at the code repositories before spring era.

Again if you would like to use Spring or not is your personal call based on project complexity. But it's other features/advantages cant be overlooked.

And XML files or Java configs are the ways of achieving spring configuration - where you would like to add your business logic is personal flavour. Only thing is you should be consistent all across your project.

2 Comments

I like your answer, and thank you. Just one question though if the repositories were flled with java code what about the configuration XML files don't they get huge too? I think what I am sensing is that we can't weed out the complexity it just gets pushed over some where else. What I mean is if a project is really big and it uses Sprimg won't the XML files get pretty big?
That's true. If you are using old spring versions where annotations are not supported, then your XML file will be huge. Otherwise it's a not big concern. I would suggest go with the latest version of spring so XML configuration concern will sorted out automatically. And you choose any version of spring (old or new), minimal basic configurations can't be avoided (xml or Java config). It is just matter of getting used to it. And benefits that you get by spring above core java are more than basic spring configurations.
2

I would suggest that you read Martin Fowler's great article on Inversion of Control and Dependency Injection to gain a better understanding of why frameworks like Spring can be really useful to solve a well known set of common dependency injection problems when writing software.

As others have mentioned, there is no obligation to use Spring; and whatever you can do with Spring, you can probably do it by other means like abstract factories, factory methods, or service locators.

If your project is small enough, then you probably wouldn't mind solving the dependency injection issues on your own using some design patterns like those mentioned above. However, depending on the size of your project, many would prefer to use a framework or a library that already packs a bunch of solutions to these recurrent head scratchers.

In regards to the advantages of dependency injection frameworks when doing unit testing is the idea that you don't need to test the dependencies of your class, but only your class.

For example, most likely your application has a layered design. It is very common to have a data access class or a repository that you use to retrieve data from a datasource. Logically, you also have a class where you use that DAO.

Evidently, you already wrote unit testing for your DAO, and therefore, when you're testing your business class (where the DAO is being used) you don't care about testing your DAO again.

Fortunately, since Spring requires some form of dependency injection for your DAO, this means your class must provide a constructor or a setter method through which we can inject that DAO into our business class, right?

Well, then during unit testing of your business class, you can conveniently use those injection points to inject your own fake DAO (i.e. a mock object). That way, you can focus on the testing of your business class and forget about retesting the DAO again.

Now compare this idea with other solutions you may have done on your own:

  • You inject the dependency directly by instantiating the DAO within your business class.
  • You use a static factory method within your code to gain access to the DAO.
  • You use a static method from a service locator within your code to gain access to the DAO.

None of these solutions would make your code easy to test because there is no simple manner to get in the way of choosing exactly what dependency I want injected into my business class while testing it (e.g. how do you change the static factory method to use a fake DAO for testing purposes?).

So, in Spring, using XML configuration or annotations, you can easily have different dependencies being injected into your service object based on a number of conditions. For example, you may have some configurations for testing that evidently would be different than those used in production. And if you have a staging environment, you may even have different XML configurations of dependencies for your application depending on whether it is running in production or integration environments.

This pluggability of dependencies is the key winning factor here in my opinion.

So, as I was saying, my suggestion to you is that you first expand your understanding of what problems Spring core (and in general all dependency injection frameworks) is trying to solve and why it matters, and that will give you a broader perspective and understanding of these problems in a way that you could to determine when it is a good idea to use Spring and when it is not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.