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

  • SEARCH
  • Home
  • 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 7605025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:03:45+00:00 2026-05-31T00:03:45+00:00

In Guice, I have a ProductionModule with my bindings. In my tests, I can

  • 0

In Guice, I have a ProductionModule with my bindings. In my tests, I can load that PLUS a module that overrides a few of the production bindings with mock objects. How do I do such a thing in spring….

For example, load production-spring.xml in test files and then have the test load in test-spring.xml which would ONLY override some of the bindings in production-spring.xml

This tests the integration and make sure changes in production-spring.xml don’t break things. These are more automated integration tests then testing a unit and work extremely well.

  • 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-31T00:03:46+00:00Added an answer on May 31, 2026 at 12:03 am

    You can override beans by listing multiple xml files. Beans in the later files will override those loaded before.

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(
          locations = {"classpath:prodDB.xml", 
                       "classpath:applicationContext.xml", 
                       "classpath:testDb.xml"})
    public class SpringTest {
    
        @Autowired
        protected DataSource dataSource; //uses the datasource from testDb.xml
    
    }
    

    So in this case testDB.xml overrides the DataSource configured in prodDb.xml. This applies even if you don’t use the SpringJUnit4ClassRunner as well:

    new ClassPathXmlApplicationContext(new String[]
            {"classpath:prodDb.xml",
             "classpath:testDb.xml"});
    

    Using a tool like Constretto you can do the same with annotated beans:

    @Service
    public class FooService...
    
    @Service
    @Environment("test")
    public class FakeFooService ...
    

    Now, if you run a test with the @Environment("test") annotation on the class, the FakeFooService will be used.

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

Sidebar

Related Questions

Currently I have 1 Guice module in my project which defines all bindings. Now
Let's say I have a Guice module ProdModule that I would like to depend
Imagine you have a PlayN game which uses Guice bindings in its core project:
Our application is a Java-GWT application that uses Guice-Persist and Guice-Servlet extensively. We have
I have a Google Appengine/Guice/Wicket Application. My problem is that due to the mapping
I'm developing a small web framework using Guice. I have a Router object that,
I have a complex set of bindings that include many private bindings to solve
I have some Guice binding code using generics that compiles and functions fine from
I have this web application that i have created using Wicket, Guice and JPA.
Does anyone have anything that is remotely helpful regarding implementation of Google Guice with

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.