testability - Testable design -


I have a Java class that has a static member using fatted (singleton).

 implements Class A interface {Private Static Datastore DB = Datastore Familiar.Just Instance ("BDB"); // Singleton Example Public Zero Save (Last String Key, Last String Val) {db.save (key, val); }}; 

Here the class A is used as the member variable for WebSwari (Stateless Bean).

I can not test this code using EZMock because there is no way to override the datastore instance.

There are two options.

  1. Give an example of a datastore to a constructor, which will be set to the DB member variable. The problem is that I do not want the webservice class to know which datastore example is created.

  2. Provide an additional safe set method to override the DB object. I used it in the same place where I am making DataStore an easy object and the member variable I'm overriding. Is this the right design?

What are the other possibilities?

You are right that the testability is bad for. Use dependency injection and do not go for stable variables:

  Public class A device interface A {Private Datastore DB; Public A (Datastore DB) {this.db = db; } ...  

To either inject or create either use dependency-injection frameworks (like spring) or make yourself an object in the bootstrap factory code.

Production code:

  New A (datastorfixed.jst instence ("...");  

Test-code:

  public void test_xxx () {DataStore db = EasyMock.createMock (DataStore.class); // some expectations and resume (dB) interface a a = new a (dB); // ...}  

Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -