lundi 28 septembre 2015

JMockit : How to inject a MockUp class into a tested class?

I'm new to JMockIt and am trying to figure out/understand how to use @Injectable for a top-level MockUp class that I have already defined.

For example: // JUnit Test Class public class RepositoryTest {

    @Tested private Repository repository;
    @Injectable private ResultsAPIWrapper resultsApiWrapper;

    @Test
    public void testRepo(){
        new ResultsApiWrapper();
        assertThat(repository.doSomething(), is("done" ) );
    }
}


// Class under test
public class Repository{

   @Autowired private ResultsAPIWrapper resultsApiWrapper;

   public String doSomething(){
      return resultsApiWrapper.load();
   }
}

// Mocked implementation of the ResultsAPIWrapper that I want injected into Repository
public class ResultsApiWrapperMock extends MockUp<ResultsAPIWrapper>{
   @Mock
   public String load(){
      return "done";
   }
}

If I try the above, I get an error

java.lang.IllegalArgumentException: Class already mocked: com.fw.wrappers.ResultsAPIWrapper
    at com.fw.wrappers.mock.ResultsApiWrapperMock.<init>(ResultsApiWrapperMock.java:12)
    at com.fw.repository.RepositoryTest.testRepo(RepositoryTest.java:38)

But If I remove the new ResultsApiWrapper() then I do not know how to specify which is the class I want to use as my mocked implementation for the autowire.

Am I misunderstanding how to do this? How can I specify that I want JMockit to autowire using my MockUp<> implementation?

Aucun commentaire:

Enregistrer un commentaire