• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dependency Injection in SCJD

 
Johan Haleby
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm used to test-driven development and Spring when coding and therefore I was thinking about creating a special class that injected dependencies to my classes in my SCJD exam (Urlybird). I know that SUN runs several automated tests on the Data class that I am to create but I've found nothing in the documentation that says that dependencies for this class are not allowed to be set after instantiation. I.e. if SUN's tests would instantiate my Data class directly in the tests it would have no dependencies by default. This way, the RandomAccessFile (my database) and other dependencies would be null. So my question is, does SUN's tests require me to instantiate a new RandomAccessFile(..) (and other dependencies as well) when they instantiate my Data class? The reason that I'd like to have some sort of dependency injection is that I want to facilitate unit testing that enables me to mock objects(and not only do integration unit testing). I could use some kind of dependency lookup instead and work my way around the problem, but this approach is not as nice.
 
Rudolph Jen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't understand what you mean exactly. Dependency Injection only means, that you don't have to find your dependencies at your own. The dependencies are giving to you. Usually best practice is to add such dependencies to your constructor.

Example:

public class MyClass {
public MyClass(final Object dependencies1) {
...
}
}

And if you have an constructor like this, the default constructor is disabled. Therefor everybody that would like to use MyClass has to use this constructor and has to provide "Object dependencies1".

In my assignment is nothing about how I have to design my architecture or even my classes so that sun can use any unknown automatic tests.

Therefor I am not having any thoughts about that. Otherwise it would be a mess implementing such an application.

Best regards,
R
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic