(Replying to my own posting)!
It turns out is was a class path ordering problem - for some reason,
putting Guice AFTER Mockito seems to be the cause
I don't understand WHY, but I have the JUnit test now working both in
Eclipse and 'outside' via Ant's JUnit task
On Friday, April 4, 2014 4:54:38 PM UTC-4, Michael Norman wrote:
>
> I have a JUnit test that uses Guice to @Inject various helpers into a
> Jersey REST resource - when I run it
> using Eclipse's built-in JUnit runner, everything works:
>
> public class MyResource {
>
> @Inject
>
> protected Helper1 helper1;
>
>
> @Inject
>
> protected Helper2 helper2;
>
> @POST
>
> @Produces("application/json")
>
> @Consumes("application/json")
>
> public Result doSomething(MyRequest request) {
>
> Result result = null;
>
> try {
>
> result = helper1.reallyDoSomething(helper2, request);
>
> }
>
> catch (Exception e) {
>
> logger.error("Error doing something", e);
>
> throw e;
>
> }
>
> return result;
>
> }
>
> }
>
> ...
>
> static Helper1 mockHelper1 = null;
> static Helper2 mockHelper2 = null;
>
> @BeforeClass
>
> public static void setUpTestServer()
>
> mockHelper1 = Mockito.mock(Helper1.class);
>
> mockHelper2 = Mockito.mock(Helper2.class);
> ... set up mock behaviour ...
>
> URI serverURI = UriBuilder.fromPath("/")
>
> .scheme("http")
>
> .host("0.0.0.0")
>
> .path(TEST_CONTEXT)
>
> .port(DEFAULT_PORT_NUMBER).build();
>
> Map<String, String> initParams = new HashMap<>();
>
> initParams.put(JAXRS_APPLICATION_CLASS, TestApplication.class
> .getName());
>
> WebappContext webapp = new WebappContext("GrizzlyContext",
> serverURI.getPath());
>
> FilterRegistration filterRegistration = webapp.addFilter(GuiceFilter.
> class.getSimpleName(),
>
> GuiceFilter.class);
>
>
> filterRegistration.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.
> class),
>
> DEFAULT_MAPPING);
>
> ServletRegistration registration = webapp.addServlet("Jersey
> ServletContainer", ServletContainer.class);
>
> registration.addMapping(DEFAULT_MAPPING);
>
> registration.setInitParameters(initParams);
>
> HttpServer server =
> GrizzlyHttpServerFactory.createHttpServer(serverURI);
>
> server.getServerConfiguration().setHttpServerName(TEST_SIMPLE_NAME+
> "Server");
>
> webapp.deploy(server);
>
> }
>
>
> public static class CloudAccountResourceTestApplication
> extendsResourceConfig {
>
> @Inject
>
> public CloudAccountResourceTestApplication(ServiceLocator
> serviceLocator) {
>
> register(JacksonObjectMapperResolver.class);
>
> //resource under test
>
> register(MyResource.class);
>
> //configure guice bridge:
>
>
> GuiceBridge.getGuiceBridge().initializeGuiceBridge(serviceLocator);
>
> GuiceIntoHK2Bridge guiceBridge =
> serviceLocator.getService(GuiceIntoHK2Bridge.class);
>
> Injector injector = Guice.createInjector(new TestGuiceModule());
>
> guiceBridge.bridgeGuiceInjector(injector);
>
> }
>
> }
>
>
> public static class TestGuiceModule extends AbstractModule {
>
> @Override
>
> protected void configure() {
>
> install(new ServletModule() {
>
> @Override
>
> protected void configureServlets() {
>
> //no-op
>
> }
>
> @Provides
>
> Helper1 provideMockHelper1() {
>
> return mockHelper1;
>
> }
>
> @Provides
>
> Helper2 provideMockHelper2() {
>
> return mockHelper2;
>
> }
>
> });
>
> }
>
> }
>
> However, when I run the same JUnit test from Ant's JUnit task, I get the
> following exception:
>
> [junit] MultiException stack 1 of 1
>
> [junit] java.lang.AssertionError: java.lang.IllegalAccessException:
> Class com.google.inject.DefaultConstructionProxyFactory$1 can not access a
> member of class com.google.inject.servlet.ManagedServletPipeline with
> modifiers "public"
>
> [junit] at
> com.google.inject.DefaultConstructionProxyFactory$1.newInstance(
> DefaultConstructionProxyFactory.java:85)
>
> [junit] at com.google.inject.ConstructorInjector.construct(
> ConstructorInjector.java:85)
>
> [junit] at com.google.inject.ConstructorBindingImpl$Factory.get(
> ConstructorBindingImpl.java:111)
>
> [junit] at com.google.inject.ProviderToInternalFactoryAdapter$1.call(
> ProviderToInternalFactoryAdapter.java:45)
>
>
>
> I believer the error is referring to a default-permission class
> (ManagedServletPipeline) that has a public constructor.
>
>
> Any help in figuring out why it works in one environment but not in
> another would be greatly appreciated!
>
> ---
>
> Mike Norman
>
>
>
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.