Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @单元测试resteasy时未注入上下文对象_Java_Unit Testing_Jboss_Jax Rs_Resteasy - Fatal编程技术网

Java @单元测试resteasy时未注入上下文对象

Java @单元测试resteasy时未注入上下文对象,java,unit-testing,jboss,jax-rs,resteasy,Java,Unit Testing,Jboss,Jax Rs,Resteasy,我正在尝试注册一个@Provider,它将把Locale对象注入@Context,这样我就可以在REST资源中重用它 在本文之后,我成功地运行了该提供程序 @Provider public class LocaleProvider implements ContainerRequestFilter { public static final String DEFAULT_LANGUAGE_CODE_VALUE = "en-US"; @Context Dispatche

我正在尝试注册一个@Provider,它将把Locale对象注入@Context,这样我就可以在REST资源中重用它

在本文之后,我成功地运行了该提供程序

@Provider
public class LocaleProvider implements ContainerRequestFilter {

    public static final String DEFAULT_LANGUAGE_CODE_VALUE = "en-US";

    @Context
    Dispatcher dispatcher;

    @Override
    public void filter(ContainerRequestContext containerRequestContext) throws IOException {
        List<Locale> acceptableLanguages = containerRequestContext.getAcceptableLanguages();
        if (acceptableLanguages == null || acceptableLanguages.isEmpty()) {
(A)         dispatcher.getDefaultContextObjects().put(Locale.class, new Locale(DEFAULT_LANGUAGE_CODE_VALUE));
            return;
        }

        dispatcher.getDefaultContextObjects().put(Locale.class, acceptableLanguages.get(0));
    }


}
到目前为止,部署到Tomcat上的代码工作得非常好。但是当我尝试对它进行单元测试时,我遇到了一个问题。我有

public class LocaleProviderTest {
    private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();

    {
        dispatcher.getRegistry().addResourceFactory(new POJOResourceFactory(Resource.class));
    }

    @Test
    public void shouldHaveDefaultLocaleWhenNoAcceptLanguageHeader() throws URISyntaxException {
        dispatcher.getProviderFactory().registerProvider(LocaleProvider.class);

        MockHttpRequest request = MockHttpRequest.get("/");
        MockHttpResponse response = new MockHttpResponse();
        dispatcher.invoke(request, response);

        assertEquals("en", response.getContentAsString());
    }
}
即使LocaleProvider在调试时执行行
(A)
,资源也会返回
null


你知道单元测试时@Context注入为什么不起作用吗?

我找到了一个基于的解决方案

在我的LocaleProvider中,而不是

 dispatcher.getDefaultContextObjects().put(Locale.class, locale);
我需要使用

 ResteasyProviderFactory.pushContext(Locale.class, locale);
希望对某人有所帮助:)

 ResteasyProviderFactory.pushContext(Locale.class, locale);