question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the appropriate way to write test in guice project?

See original GitHub issue

I feel apologetic to ask for help in github issue. I am new in google guice and got stuck in writing test for a guice project.

So for example I have a singleton bean:

@Singleton
class A {
    public String hello() {
        return "helloworld";
    }
}

What is the appropriate way to write unit test for A class? My experience is that I should get the bean without loading the whole context and test its behavior separately. This is how I can test it in a Spring and Mockito way:

public class ATest {
    @InjectMocks
    private A a;

    @Test
    public void testAIsPresent() {
        assertNotNull(a);
    }
}

I believe there should be a similar way in Guice to write test like this. But I search anywhere and could not find any workable approach. Am I doing it in a wrong way in Guice? Is there any docs for writing test in guice? Thanks so much.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
cardamoncommented, Jul 6, 2017

The only mention in the docs AFAIK is the recommendation to use a package private constructor, to balance the best practice of keeping constructors as hidden as possible against testability. https://github.com/google/guice/wiki/KeepConstructorsHidden

Example:

public class FooBar {
  private FooService fooService;
  private BarManager barManager;

  @Inject FooBar(FooService fooService, BarManager barManager) {
    this.fooService = fooService;
    this.barManager = barManager;
  }

  int compute() {
    return fooService.getSomething() + barManager.getSomethingElse();
  }
}

public class Test {
  @Mock FooService fooService;
  @Mock BarManager barManager;

  @Test public void testCompute() {
    // stubbing method calls
    when(fooService.getSomething()).thenReturn(41);
    when(barManager.getSomethingElse()).thenReturn(1);

    // direct instantiation 
    FooBar fooBar = new FooBar(fooService, barManager);

    assertEquals(42, fooBar.compute());
  }
}
2reactions
dimo414commented, Jul 7, 2017

For unit tests you shouldn’t need to use Guice at all; just construct your subject under test as normal, passing in mocks/fakes/etc. as needed.

For integration tests just create an Injector that depends on the modules you’re trying to test, and replace “production” modules with “testing” modules as necessary. BoundFieldModule is nice for binding mocks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to test implementations of Guice AbstractModule?
Typically the best way to test Guice modules is to just create an injector in your test and ensure you can get instances...
Read more >
Dependency Injection: Guice for Unit Testing - Part 2
This is a best practice pattern when writing the unit test cases. However, for more complicated project, we should use Guice to create...
Read more >
Guicing Up Your Testing | Developer.com
To illustrate the use of Guice, you need an example unit testing setup. ... API for configuring the modules to wire up objects...
Read more >
Component Tests (with Guice) - OpenDaylight Documentation
They focus on testing the code in their own module, and typically stub required external ... You can write Guice object binding wiring...
Read more >
Java Testing With Guice - 2.8.x - Play Framework
If you're using Guice for dependency injection then you can directly configure how components and applications are created for tests. This includes adding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found