用会话数据测试播放控制器

我有一个简单的控制器测试。用会话数据测试播放控制器

route(fakeRequest(routes.Accounts.accounts()).session("sessionref","fakeSession")); 

担保Autheticator看起来是这样的:

public class Secured extends play.mvc.Security.Authenticator { 

@Inject

AuthServices authService;

public String getUsername(Http.Context context) {

return authService.checkSession(context);

}

@Override

public Result onUnauthorized(Http.Context context) {

return ok(index.render(formFactory.form(forms.LoginForm.class)));

}

}

我怎么能嘲笑authService? 我试图与吉斯绑定嘲笑,但这种方法不工作

@Before 

public void setup() {

startPlay();

MockitoAnnotations.initMocks(this);

Module testModule = new AbstractModule() {

@Override

public void configure() {

bind(AuthServices.class)

.toInstance(authServices);

}

};

GuiceApplicationBuilder builder = new GuiceApplicationLoader()

.builder(new play.ApplicationLoader.Context(Environment.simple()))

.in(Mode.TEST)

.overrides(testModule);

Guice.createInjector(builder.applicationModule()).injectMembers(this);

}

回答:

您可以为测试游戏控制器读取this并按照this example与吉斯测试。

对于你的情况是这样的:

public class MyTest extends WithApplication { 

@Mock

AuthServices mockAuthService;

@Override

protected Application provideApplication() {

return new GuiceApplicationBuilder()

.overrides(bind(CacheProvider.class).toInstance(mockAuthService))

.in(Mode.TEST)

.build();

}

@Before

public void setup() {

MockitoAnnotations.initMocks(this);

}

@Test

public void testAccounts() {

running(provideApplication(),() -> {

RequestBuilder testRequest = Helpers.fakeRequest(controllers.routes.Accounts.accounts()).session("sessionref","fakeSession");

Result result = route(testRequest);

//assert here the expected result

});

}

}

以上是 用会话数据测试播放控制器 的全部内容, 来源链接: utcz.com/qa/264935.html

回到顶部