1


I am trying to mock the gae user service for writing unit tests and couldn't get the following code to work.

My test class is as below.

public class AuthenticationTest {

    private final LocalServiceTestHelper helper =
        new LocalServiceTestHelper(new LocalUserServiceTestConfig())
            .setEnvIsAdmin(true).setEnvIsLoggedIn(true)
            .setEnvEmail("test@example.com");

    @Before
    public void setUp() {
        helper.setUp();
    }

    @After
    public void tearDown() {
        helper.tearDown();
    }

    @Test
    public void testIsAdmin() {
        UserService userService = UserServiceFactory.getUserService();
        assertTrue(userService.isUserAdmin());

        String email = userService.getCurrentUser().getEmail();
        assertEquals(email, "test@example.com");
    }
}

I find that userService.getCurrentUser() always returns null.

Most of the code is taken from the example in developers.google.com. The only thing I added is call to .setEnvEmail("test@example.com")

Any help would be appreciated.

Thanks,
Sathya

4

1 回答 1

2

为了模拟电子邮件,还需要模拟身份验证域,只需添加

.setEnvAuthDomain("example.com");

到您的助手初始化或@Before 方法后初始化,它会正常工作。

希望能帮助到你

于 2013-05-12T16:28:59.857 回答