我正在使用Jukito测试 GWTP 演示者,但似乎无法让命名绑定工作。
我希望能够测试onReveal()
我的登录小部件,因此我需要提供我CurrentUserDto
已登录和未登录的副本。这就是我想要做的:
@RunWith(JukitoRunner.class)
public class LoginWidgetPresenterTest
{
public static class Module extends JukitoModule
{
@Override
protected void configureTest()
{
bind(CurrentUserDto.class).annotatedWith(Names.named("loggedIn")).toProvider(LoggedInUserProvider.class);
bind(CurrentUserDto.class).annotatedWith(Names.named("loggedOut")).toProvider(LoggedOutUserProvider.class);
// these don't work either:
// bindNamed(CurrentUserDto.class, "loggedIn").toProvider(LoggedInUserProvider.class);
// bindNamed(CurrentUserDto.class, "loggedOut").toProvider(LoggedOutUserProvider.class);
}
public static class LoggedInUserProvider implements Provider<CurrentUserDto>
{
@Override
public CurrentUserDto get()
{
CurrentUserDto currentUser = new CurrentUserDto(true, 100L); // isLoggedIn, userId
currentUser.setEmail("john@example.com");
currentUser.setNickname("john");
currentUser.setLogoutUrl("#logout");
return currentUser;
}
}
public static class LoggedOutUserProvider implements Provider<CurrentUserDto>
{
@Override
public CurrentUserDto get()
{
CurrentUserDto currentUser = new CurrentUserDto(false, null); // isLoggedIn, userId
return currentUser;
}
}
}
@Inject
LoginWidgetPresenter presenter;
@Test
public void testLoggedInUser(LoginWidgetPresenter.MyView myView, @Named("loggedIn") CurrentUserDto currentUser)
{
// when
presenter.onReveal();
// then
verify(myView).displayEmail(currentUser.getEmail());
verify(myView).displayLogoutLink(currentUser.getLogoutUrl());
}
@Test
public void testLoggedOutUser(LoginWidgetPresenter.MyView myView, @Named("loggedOut") CurrentUserDto currentUser)
{
// when
presenter.onReveal();
// then
verify(myView, never()).displayEmail(currentUser.getEmail());
verify(myView, never()).displayLogoutLink(currentUser.getLogoutUrl());
}
}
我怀疑这可能与范围有关,但我是依赖注入的新手,所以我仍然对它有所了解。
当我只有一个提供者时它工作得很好,现在我使用@Named
的CurrentUserDto
对象LoginWidgetPresenter
总是为 null。