Announcement Announcement Module
Collapse
No announcement yet.
transactional JUnit4 controller testing with multiple DataSources Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • transactional JUnit4 controller testing with multiple DataSources

    I'm attempting to write a test as an extension of AbstractTransactionalJUnit4SpringContextTests for a SimpleFormController. I happen to have two DataSources in my application context: one used by the controller via setter injection and another unrelated to this particular controller. When the test runs, Spring doesn't know which one to inject into my AbstractTransactionalJUnit4SpringContextTests object's dataSource field.

    The test throws this exception:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'web.authmgr.test.TestAddUserForm': Autowiring of methods failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2: [authModifyDataSource, authDataSource]
    How do I tell it which one to use? "authModifyDataSource" is the one I want.

  • #2
    You should probably wire it "by name" instead of the default "by type".

    If you use annotations to configure wiring, you could use the @Resource annotation, as described in http://static.springframework.org/sp...rce-annotation.

    If you use an XML based configuration, you could use the attribute autowire="byname", as described in http://static.springframework.org/sp...ctory-autowire

    Comment


    • #3
      I was thinking along those lines.... I see how using @Resource would work for a property I had declared, but I'm not sure how to use @Resource with a property (dataSource) declared in the parent class.

      I tried overriding the setDataSource method like so:

      Code:
      	@Resource(name="authModifyDataSource")
      	public void setDataSource(DataSource d) {
      		super.setDataSource(d);
      	}
      but I still get the same error.

      Comment


      • #4
        work around...

        Hey bart, here's a work around.
        Instead of extending directly from AbstractJUnit4SpringContextTests, extend from AbstractJUnit4SpringContextTests... Here's my class:
        Code:
        @TestExecutionListeners({TransactionalTestExecutionListener.class})
        @Transactional
        
        @RunWith(SpringJUnit4ClassRunner.class)
        @ContextConfiguration(locations = {
                "/com/telcel/activaciones/dominio/domainContext-keywords.xml",
                "/com/telcel/activaciones/dominio/domainContext-resources.xml"})
        public class ServExpressTdmaTests extends AbstractJUnit4SpringContextTests {
        
            protected SimpleJdbcTemplate simpleJdbcTemplate;
        
            @Resource(name = "servExpressDs")
            public void setDataSource(final DataSource dataSource) {
                this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
            }
        This way you can wire your data source the way you please ...

        Good luck

        Comment

        Working...
        X