我有以下類:嘲諷的Spring bean
public class Plugin {
private DistributionManager manager;
public void init(){
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
manager = context.getBean(DistributionManager.class);
}
public String doSomething(){
String s = manager.doSomething();
return doSomethingElse(s);
}
DistributionManager
類本身有很多自動裝配Autowired依賴並標記爲@Component
現在我想運行一些單元測試的所有代碼:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
public class PluginTestCase extends AbstractJUnit4SpringContextTests{
@Resource
DistributionManager manager;
@Test
public void testDoSomething(){
Plugin plugin = mock(Plugin.class);
//how can I inject DistributionMamanger bean to plugin using mockito?
assertEquals("MyResult", plugin.doSomething());
}
}
我從來沒有使用過mockito。你能幫我模擬插件並完成這個單元測試嗎?
更新:
我根據建議嘗試以下測試:
@RunWith(MockitoJUnitRunner.class)
public class PluginTestCase {
@Mock
DistributionManager manager;
@InjectMocks
Plugin testedPlugin;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testDao(){
testedPlugin.init();
testedPlugin.doSomething();
}
}
,但是,我有以下異常:
org.mockito.exceptions.base.MockitoException: Field 'testedPlugin' annotated with @InjectMocks is null.
Please make sure the instance is created *before* MockitoAnnotations.initMocks();
Example of correct usage:
class SomeTest {
@InjectMocks private Foo foo = new Foo();
@Before public void setUp() {
MockitoAnnotations.initMock(this);
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.withBefores(JUnit45AndHigherRunnerImpl.java:27)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:261)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
如果你想單元測試'Plugin'類,那你爲什麼要嘲笑它呢? – skaffman
@scaffman,可能是我錯了,我從來沒有用過嘲笑。我應該嘲笑什麼? –
我不知道,你還沒有告訴我們你想要測試什麼。您可能想要閱讀Mockito主頁上的簡單教程,以更好地瞭解您想要實現的目標。 – skaffman