ユニットテストでUserのインスタンスを取得する
ローカルのユニットテストで、com.google.appengine.api.users.Userのインスタンスを取得しようとしても、デフォルト・コンストラクタはプライベートであるし、UserServiceのgetCurrentUser()メソッドもnullを返すため、簡単ではない。
以下のリンクを参考にすれば、getCurrentUser()メソッドで任意のuserIdを持つUserのインスタンスを取得することが可能。
Local Unit Testing with UserService: userId==null problem
また、slim3を利用する場合は、下記のようにtester.environmentからorg.slim3.tester.TestEnvironmentを利用するらしい。
public class UserTest extends AppEngineTestCase { @Before @Override public void setUp() throws Exception { super.setUp(); Map<String, Object> envAttributes = new HashMap<String, Object>(); envAttributes.put( "com.google.appengine.api.users.UserService.user_id_key", "42"); tester.environment.setEmail("admin@example.com"); tester.environment.setAttributes(envAttributes); } @Test public void instantiateUser() { UserService service = UserServiceFactory.getUserService(); User user = service.getCurrentUser(); assertThat(user, is(notNullValue())); assertThat(user.getUserId(), equalTo("42")); assertThat(user.getEmail(), equalTo("admin@example.com")); } }