What is the appropriate way to write test in guice project?
See original GitHub issueI 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:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
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.