2

I have a class annotated as follows :

@Singleton
public class Foo{
   //...
   private Too too;
   public Too getToo(){
   }

}

And this class is an attribute in an abstract class which has the visibility protected.

public abstract class AbstractUsingFoo {
   @Inject
   protected Foo foo;

   public methodToTestPolymorphism();

}

Then there are the subclasses.

How can I have a single instance for Foo class using EasyMock?

I have no problem to combine with Spring4Junit.

Thank you a lot!

  • 1
    Can't help you with easymock, but somehow the title of this question is a bit vague. Could you maybe make it a bit more specific to your question. Also adding easymock as tag could be a good idea. – NoDataDumpNoContribution Jun 03 '14 at 08:50
  • Hello! It was a problem at SOF cookies management I guess. Because I was writing another question about WS producer/consumer. When I submitted the other question, this one got submitted automaticly. :) the title corrected now –  Jun 03 '14 at 08:54
  • are you using `google guice` framework? – Ankit Kumar Jun 03 '14 at 10:55
  • @dreamer no. What is google guice for? I use CDI/EJB. and for unit test : JUnit, EasyMock, and if necessary Spring4Junit. –  Jun 03 '14 at 11:46

1 Answers1

1

Code containing singletons is notoriously hard to test because it's awkward to mock out the singleton. The code needs to be designed with testability in mind.

Can you use dependency injection to make the code easier to test?

Also see the accepted answer to this question Unit testing with singletons

Community
  • 1
  • 1
andrea
  • 481
  • 2
  • 8
  • 27