Playframework FakeRequest返回404
我正在尝试使用Play测试登录表单处理程序!然而,在执行测试时,我总是得到404作为状态代码Playframework FakeRequest返回404,playframework,playframework-2.4,Playframework,Playframework 2.4,我正在尝试使用Play测试登录表单处理程序!然而,在执行测试时,我总是得到404作为状态代码 @Test public void testAuthenticate() { HashMap<String, String[]> formData = new HashMap<>(); String[] email = {"admin@domain.com"}; String[] password = {"password123"}; formDa
@Test
public void testAuthenticate() {
HashMap<String, String[]> formData = new HashMap<>();
String[] email = {"admin@domain.com"};
String[] password = {"password123"};
formData.put("email", email);
formData.put("password", password);
Http.RequestBuilder request = Helpers.fakeRequest("POST", "/app-service/login").bodyFormArrayValues(formData);
Result result = route(request, maxTimeout);
assertNotNull(result);
assertEquals(OK, result.status());
}
我在FakereRequest控件中输入的URI是否错误,或者是否缺少其他内容
谢谢我最终选择了一种不同的方法在Play框架中测试表单数据。也许没有那么优雅,但它确实做到了
@Test
public void testAuthenticate() {
HashMap<String, String> formData = new HashMap<>();
formData.put("email", "admin@domain.com");
formData.put("password", "password123");
Http.RequestBuilder request = new Http.RequestBuilder().bodyForm(formData);
Helpers helpers = new Helpers();
Callable<Result> callable = new Callable() {
@Override
public Object call() throws Exception {
F.Promise promise = controller.authenticate();
return promise.get(maxTimeout);
}
};
Result result = helpers.invokeWithContext(request, callable);
assertNotNull(result);
assertEquals(OK, result.status());
}
@测试
公共无效遗嘱认证(){
HashMap formData=新HashMap();
formData.put(“电子邮件”admin@domain.com");
formData.put(“密码”、“密码123”);
Http.RequestBuilder request=new Http.RequestBuilder().bodyForm(formData);
Helpers Helpers=新的Helpers();
Callable Callable=new Callable(){
@凌驾
公共对象调用()引发异常{
F.Promise=controller.authenticate();
返回promise.get(maxTimeout);
}
};
结果=helpers.invokeWithContext(请求,可调用);
assertNotNull(结果);
assertEquals(OK,result.status());
}
即使这是一个老问题,我也遇到了完全相同的问题并找到了解决方案。这是在我如何运行我的测试
因此,我发现的主要问题是对routes文件的修改没有被执行。我的测试都通过了一次sbt运行
来手动测试我的API
问题出在我的IntelliJ SBT设置中。要修复所有问题(意味着确保IntelliJ构建我的项目时,它实际上在后台调用sbt compile
),只需在“构建、执行、部署/构建工具/sbt”设置窗口中选中“使用sbt shell进行构建和导入”,然后瞧
@Test
public void testAuthenticate() {
HashMap<String, String> formData = new HashMap<>();
formData.put("email", "admin@domain.com");
formData.put("password", "password123");
Http.RequestBuilder request = new Http.RequestBuilder().bodyForm(formData);
Helpers helpers = new Helpers();
Callable<Result> callable = new Callable() {
@Override
public Object call() throws Exception {
F.Promise promise = controller.authenticate();
return promise.get(maxTimeout);
}
};
Result result = helpers.invokeWithContext(request, callable);
assertNotNull(result);
assertEquals(OK, result.status());
}