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 7887591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:37:11+00:00 2026-06-03T05:37:11+00:00

I recently cam across a class like this: public class SomeDao { @Inject private

  • 0

I recently cam across a class like this:

public class SomeDao {
   @Inject
   private DataSource dataSource;

   @Value("#{someMap['someDao.sql']}")
   private String sql;

   private JdbcTemplate jdbcTemplate;

   @PostConstruct
   private void postConstruct() {
      jdbcTemplate = new JdbcTemplate(dataSource);
   }

   ...
}

Now I would like to unit test this class by injecting the DataSource and the SQL string. As far as I can see, there are two options for this:

  1. Load up an application context which will be slow and most likely load a whole lot of stuff that I don’t need for the test
  2. Use reflection to set the private properties and call the private postConstruct() method

In the days before spring annotations, the class would have been written like this:

public class SomeDao {
   private String sql;
   private JdbcTemplate jdbcTemplate;

   public SomeDao(DataSource dataSource, String sql) {
      this.jdbcTemplate = new JdbcTemplate(dataSource);
      this.sql = sql;
   }    
   ...
}

And I could have easily tested this class without the need for reflection or spring. My class was a pure pojo and had no spring dependencies.

So, are spring annotations a good thing or are they a step backwards? Are there times where I shoulud be using them and times when I should use the old XML app context?

Thanks,
Lance.

  • 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-06-03T05:37:12+00:00Added an answer on June 3, 2026 at 5:37 am

    Why not declare a test-context with mocks of beans and inject those needed by your class from that? That’s what people usually do and it’s quite straightforward.

    The most lightweight way of doing this is to provide an inner class inside your test class, annotated with @Configuration that has methods that provide mocks:

    @Configuration
    public class DataAccessConfiguration {
    
        @Bean
        public DataSource dataSource() {
            DataSource dataSource =  mock(Datasource.class);
            return dataSource;
        }
    
        @Bean
        public Map<String,String> someMap() {
            Map<String, String> map = new HashMap<>();
            map.put("someDao.sql", "somevalue");
            return map;
        }
    
    }
    

    So instead of giving up on autowiring, you can actually take advantage of it. Also you limit the loaded context to just what is needed by the class under test.

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

Sidebar

Related Questions

I cam across this recently : here public int print(Graphics g, PageFormat pf, int
recently, while working on a db2 -> oracle migration project, we came across this
I cam across a Java related question recently that was (to paraphrase): What reasons
Recently, I came across this one stone kills multiple birds framework - http://phonegap.com/ .
Recently i cam across a statements : import static java.lang.System.out; import static java.lang.System.exit; I
Recently, I've discovered this code of the following structure: Interface : public interface Base<T>
Recently, I was writing a class in which I discovered that I could reduce
Recently just upgraded to SQL Server 2008 R2 Express. When I attempt to create
Recently, we discovered odd behavior in some old code. This code has worked for
Recently I've been doing quite the project mostly working with the DateTime class. Now,..

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.