Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiating a test configuration instance from a test yaml file. #61

Closed
patricioe opened this issue May 31, 2015 · 4 comments
Closed

Comments

@patricioe
Copy link

Hi all, I'm having issues instantiating a test configuration instance for my local dev environment.

after following the example here: https://github.com/HubSpot/dropwizard-guice/blob/master/src/test/java/com/hubspot/dropwizard/guice/GuiceBundleTest.java

(in that example the author does a "new Configuration()" but I interested in loading the test info located at "config-test.yml" instead.

I wrote this class:

@RunWith(MockitoJUnitRunner.class)
public class GuiceBundleTest {

   Environment environment;

    private GuiceBundle<Configuration> guiceBundle;

    @ClassRule
    public static final DropwizardAppRule<APIConfigurationForTest> RULE =
            new DropwizardAppRule<APIConfigurationForTest>(TestApplication.class, ResourceHelpers.resourceFilePath("config-test.yml"));

    @Before
    public void setUp() {
        environment = RULE.getEnvironment();
        guiceBundle = GuiceBundle.newBuilder()
                .addModule(new TestModule(RULE.getConfiguration()))
                .build();
        Bootstrap bootstrap = mock(Bootstrap.class);
        guiceBundle.initialize(bootstrap);
        guiceBundle.run(RULE.getConfiguration(), environment);
    }
    ....

and got a:

java.lang.IllegalStateException: Started
    at org.eclipse.jetty.servlet.BaseHolder.illegalStateIfContextStarted(BaseHolder.java:181)
    at org.eclipse.jetty.servlet.FilterHolder$Registration.addMappingForUrlPatterns(FilterHolder.java:238)
    at com.hubspot.dropwizard.guice.JerseyUtil.registerGuiceFilter(JerseyUtil.java:69)
    at com.hubspot.dropwizard.guice.GuiceBundle.run(GuiceBundle.java:115)
    at io.codigo.guice.GuiceBundleTest.setUp(GuiceBundleTest.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)

I spend around 2 hours trying to figure this out. If you have any hint on how to get the server to start please let me know.

Thanks

@mrserverless
Copy link
Contributor

Are you trying to test the GuiceBundle or are you trying to do integration testing on your TestApplication? Your code seem to be trying to do both which is rather confusing.

Have a look at https://github.com/HubSpot/dropwizard-guice/blob/master/src/test/java/com/hubspot/dropwizard/guice/InjectedIntegrationTest.java instead

@patricioe
Copy link
Author

Hi yunspace. Thank you for your response. I'm trying to do some integration test set up to be able to test DAOs against a test environment.

I'll take a look at your suggestion.
Thank you.

@patricioe
Copy link
Author

@yunspace what's the best pattern to initialize my module so my DAO or controller get injected using the test env?

@mrserverless
Copy link
Contributor

Can you clarify what is this test env that you are trying to inject? A Hibernate DAO for example only requires SessionFactory to be injected. If you want to connect to a test database, that information is configured in your config.yml file which is used by HibernateBundle, not injected into your DAO.

I would suggest maybe try:

  1. get your application running first without dependency injection.
  2. once you understand your dependencies, add Guice to your applicaiton
  3. when all that works, add Guice to your integration tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants