Coul不会使用@ContextConfiguration在测试类中注入属性

时间:2013-12-23 09:20:53

标签: java spring spring-test

我有测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:test-application-context.xml" })
@Transactional
@TransactionConfiguration(transactionManager = "testTransactionManager", defaultRollback = true)
public class PostDAODatabaseTest {

    @Autowired
    PostDAO dao;

    @Test
    public void testFindAll() throws Exception {
        List<Post> posts =  dao.findAll();
        assertNotNull(posts);
    }

    @Test
    public void testAddPost() throws Exception {
        Post post = new Post();
        post.setId(150);
        post.setAuthor("Jack C.");
        post.setText("Test text passed");
        dao.addPost(post);
    }

    @Test
    public void testDeletePost() throws Exception {
        int fifthPostId = 5;
        dao.deletePost(fifthPostId);
    }

}

和test-application-context

<context:annotation-config />
    <context:component-scan base-package="com.github.futu" />   

    <tx:annotation-driven transaction-manager="testTransactionManager"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property 
        name="url" value="jdbc:mysql://localhost:3306/blog" /> <property name="username" 
        value="blogger" /> <property name="password" value="blogger" />
    </bean>

    <bean id="com.github.futu.dao.PostDAO" class="com.github.futu.dao.impl.PostDAODatabase"> 
        <property name="dataSource" ref="dataSource" /> 
    </bean>

    <bean id="com.github.futu.dao.AuthorDAO" class="com.github.futu.dao.impl.AuthorDAODatabase">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="validator" class="com.github.futu.validator.PostValidator" />     

    <bean id="testTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

但它失败了以下异常

No matching bean of type [com.github.futu.dao.AuthorDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

但它在我的测试应用程序上下文中声明了

<bean id="com.github.futu.dao.AuthorDAO" class="com.github.futu.dao.impl.AuthorDAODatabase">
            <property name="dataSource" ref="dataSource" />
        </bean>

我尝试通过新操作符

初始化它

AuthorDAO dao = new AuthorDAODatabase();但在这种情况下,它无法创建事务管理器。

1 个答案:

答案 0 :(得分:0)

我找出原因

Maven找不到test-application-context

我把它放在src / test / resources

并添加到pom

<resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/test/resources</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
            <excludes>
                <exclude>**/*local.properties</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>