Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:41:34+00:00 2026-05-14T23:41:34+00:00

I’d like to run a unit test where a constant is slightly different than

  • 0

I’d like to run a unit test where a constant is slightly different than in the standard version. That is, in my default module, the following is

bindConstant().annotatedWith(Names.named("number of players")).to(4);

but in testing, I’d like to try this line instead:

bindConstant().annotatedWith(Names.named("number of players")).to(2);

Id like to achieve that without copying all of the rest of the module. What I really want is a “default” module that is “below” a more specialized module, such that in case of conflict, the specialized module wins (instead of throwing an exception, which is what guice does).

In essence, my question is: how does anybody arrange for more than one module without lots of code duplication?

Update: I’ve realized that the solution is really to use a factory, rather than a constant in THIS use case. I’d still be interested to learn if there’s something like hierarchies of modules in general, though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:41:35+00:00Added an answer on May 14, 2026 at 11:41 pm

    Typically when using Guice properly, you shouldn’t need to use Guice at all in tests (particularly unit tests… integration and end-to-end tests, yes). Anyway:

    I’m not sure I understand what you’re trying to do or what the issue is exactly but… you realize that when creating an Injector you can provide any number of Modules to it, right? That’s a key part of using Guice. Make the modules as course or fine-grained as you want. You could have a NumberOfPlayersModule that only has that one binding, and then use a different module with a different binding instead sometimes (like for your test). You could also make a module that takes a constructor argument and create the module as new NumberOfPlayersModule(4) or new NumberOfPlayersModule(2) as you like.

    There’s also another feature of Guice that lets you override bindings in one or more modules with bindings from one or more other modules. That works like this:

    // FooModule is your module that contains the "number of players" binding and
    // some others
    
    Module override = Modules.override(new FooModule())
       .with(new AbstractModule() {
          protected void configure() {
             bindConstant().annotatedWith(Names.named("number of players")).to(2);
          }
       });
    Injector injector = Guice.createInjector(override);
    // The int @Named("number of players") for the injector is 2
    

    As you can see, there are really quite a lot of ways to make configuring your application different ways easy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.