我以下這個教程Why should we use dependency injection?和我有這個類圖 錯誤檢測吉斯dependeing注射
和我有以下的JUnit代碼來測試模型。
public class TestPerson {
private Injector injector;
@Before
public void init(){
injector = Guice.createInjector(new AbstractModule(){
@Override
protected void configure() {
bind(MessageService.class).to(MockService.class);
}
});
}
@Test
public void tetGreetFriend(){
Person person = injector.getInstance(Person.class);
person.gretFriends();
MockService mockService = injector.getInstance(MockService.class);
assertEquals("Hello my Friend", mockService.msg);
}
}
當我開始測試,我得到以下錯誤
java.lang.AssertionError: expected:<hello world> but was:<null>
這將是我的代碼的問題。我是新來的IoC和DI
請分享相關類的代碼,而不是圖 – nullpointer
此外,錯誤陳述與您的代碼相矛盾,根據'Assert'中的'expected <>' – nullpointer