0

我正在使用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());

    }

}

我怀疑这可能与范围有关,但我是依赖注入的新手,所以我仍然对它有所了解。

当我只有一个提供者时它工作得很好,现在我使用@NamedCurrentUserDto对象LoginWidgetPresenter总是为 null

4

2 回答 2

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;
        }
}
于 2014-06-10T03:06:05.830 回答
0

我假设LoginWidgetPresenter有一个裸CurrentUserDto注入。测试方法的参数不会改变LoginWidgetPresenter. 要处理这个问题,您可以有两个不同的模块,每个模块绑定CurrentUserDto到不同的Provider.

于 2014-06-09T21:03:06.107 回答