我正在使用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
个对象始终为空。
答案 0 :(得分:1)
以下应该有效,因为Jukito支持@Provides:
public static class Module extends JukitoModule
{
@Override
protected void configureTest()
{
}
@Provides
@Named("loggedIn")
CurrentUserDto getLoggedInDto()
{
CurrentUserDto currentUser = new CurrentUserDto(true, 100L); // isLoggedIn, userId
currentUser.setEmail("john@example.com");
currentUser.setNickname("john");
currentUser.setLogoutUrl("#logout");
return currentUser;
}
@Provides
@Named("loggedOut")
CurrentUserDto getLoggedOutDto()
{
CurrentUserDto currentUser = new CurrentUserDto(false, null); // isLoggedIn, userId
return currentUser;
}
}
答案 1 :(得分:0)
我认为LoginWidgetPresenter
已经注入了CurrentUserDto
。测试方法的参数不会更改LoginWidgetPresenter
的依赖关系。要处理此问题,您可以使用两个不同的模块,每个模块将CurrentUserDto
绑定到不同的Provider
。