Java Play2 Spring测试 - 测试上下文依赖注入?

时间:2014-10-07 21:06:25

标签: java unit-testing playframework

说我有一个播放全局文件......

public class Global extends GlobalSettings {

    private ApplicationContext ctx;

    @Override
    public void onStart(Application app) {
        ctx = new ClassPathXmlApplicationContext("context/components.xml");
    }

    @Override
    public <A> A getControllerInstance(Class<A> clazz) {
        return ctx.getBean(clazz);
    }
}

我有这样的测试...

@Test
public void itShouldFailOnMissingFields() throws Exception {
    running(fakeApplication(), () -> {
                Map<String, String> body = new HashMap<String, String>();
                body.put("email", "jason@goodwin.com");
                body.put("password", "ro");
                FakeRequest request = new FakeRequest(POST, "/v1/profile")
                        .withFormUrlEncodedBody(body);
                Result result = route(request);
                assertEquals(status(result), 400);
            }
    );
}

如何将测试依赖项注入控制器?我还有其他方法可以测试吗?

1 个答案:

答案 0 :(得分:0)