我是JMockIt的新手,正在努力理解如何在已定义的顶级MockUp类中使用@Injectable
。
例如:
// 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";
}
}
如果我尝试上述方法,会出现错误。
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)
但是,如果我删除new ResultsApiWrapper()
,我就不知道如何指定哪个类作为我的模拟实现来进行自动装配。
我是否误解了如何做到这一点?我该如何指定我要使用MockUp<>
实现来进行JMockit的自动装配?