基本测试类中的@Value注释不从属性文件中注入值

时间:2012-10-05 16:22:59

标签: java spring spring-annotations

出于某种原因,我没有将@Value注释注入到我的类中,而是将其用作其他测试类的基础。我是Spring的新手,我对Java的了解还不是最好的。我相信对于有更多经验的人来说这会更加明显,但我无法理解这一点。

如果我注释掉setDbProperties()方法,则会填充字段,但仅使用@Value注释,我会在字段中显示空值。

这是我的基础测试课程:

package com.blah;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:/WEB-INF/application-context.xml",
        "classpath:/WEB-INF/application-context-test.xml" })
@ActiveProfiles("test")
public abstract class BaseDbTestCase extends DBTestCase {

    @Value("${test.db.connection.url}")
    private String connectionUrl;

    @Value("${test.db.driver.class}")
    private String driverClass;

    @Value("${test.db.username}")
    private String dbUserName;

    @Value("${test.db.password}")
    private String dbPassword;

    @Value("${test.db.datasource.path}")
    private String dataSource;

    public BaseDbTestCase(String name) throws IOException {

        super(name);

        // setDbProperties();

        System.setProperty(
                PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,
                driverClass);
        System.setProperty(
                PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,
                connectionUrl);
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,
                dbUserName);
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,
                dbPassword);
    }

    @After
    public void after() throws Exception {
        DatabaseOperation.DELETE_ALL.execute(getConnection(), getDataSet());
    }

    @Override
    protected IDataSet getDataSet() throws Exception {
        return new FlatXmlDataSetBuilder()
                .build(new FileInputStream(dataSource));
    }

    private void setDbProperties() throws IOException, FileNotFoundException {

        Properties properties = new Properties();

        properties.load(new FileInputStream(
                "src/main/java/resources/application.properties"));

        connectionUrl = properties.getProperty("test.db.connection.url");
        driverClass = properties.getProperty("test.db.driver.class");
        dbUserName = properties.getProperty("test.db.username");
        dbPassword = properties.getProperty("test.db.password");
        dataSource = properties.getProperty("test.db.datasource.path");
    }

    @Override
    protected DatabaseOperation getSetUpOperation() throws Exception {
        return DatabaseOperation.REFRESH;
    }

    @Override
    protected DatabaseOperation getTearDownOperation() throws Exception {
        return DatabaseOperation.NONE;
    }

    @Before
    public void init() throws Exception {
        DatabaseOperation.REFRESH.execute(getConnection(), getDataSet());
    }

}

如您所见,其他Spring(和非Spring)注释在此类中有效。

我的应用程序上下文中有<context:property-placeholder location="classpath:/resources/application.properties" />

此外,@Value注释在扩展BaseDbTestCase类的类中工作正常。

TIA

更新 在采用@Biju的建议将系统属性的设置移动到init()方法(用@Before注释)后,我现在得到以下堆栈跟踪。基于调试,在抛出错误之前似乎没有超过构造函数。

org.dbunit.assertion.DbAssertionFailedError: driverClass is null
    at org.dbunit.assertion.DefaultFailureHandler$DefaultFailureFactory.createFailure(DefaultFailureHandler.java:265)
    at org.dbunit.assertion.DefaultFailureHandler.createFailure(DefaultFailureHandler.java:110)
    at org.dbunit.assertion.SimpleAssert.fail(SimpleAssert.java:90)
    at org.dbunit.assertion.SimpleAssert.assertTrue(SimpleAssert.java:77)
    at org.dbunit.assertion.SimpleAssert.assertNotNullNorEmpty(SimpleAssert.java:61)
    at org.dbunit.JdbcDatabaseTester.<init>(JdbcDatabaseTester.java:103)
    at org.dbunit.PropertiesBasedJdbcDatabaseTester.<init>(PropertiesBasedJdbcDatabaseTester.java:68)
    at org.dbunit.DBTestCase.newDatabaseTester(DBTestCase.java:70)
    at org.dbunit.DatabaseTestCase.getDatabaseTester(DatabaseTestCase.java:109)
    at org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:151)
    at junit.framework.TestCase.runBare(TestCase.java:132)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 个答案:

答案 0 :(得分:0)

我认为是因为当您根据BaseDbTestCase派生新的测试用例时,BaseDbTestCase中的@ContextConfiguration在派生的测试用例中并不真实可见 - @ContextConfiguration具有在最后的测试案例中。

<强>更新

我上面提到的是不正确的,我测试了一次,看起来@ContextConfiguration@RunWith即使在基类中也足够了,它正确地注入@Value字段在基类中。

现在您遇到的问题是因为您希望变量在构造函数中可用 - 它们不会,因为@Value在创建实例后被解析 - 所以在您的情况下,因为您期望它出现在构造函数中它会失败。您应该能够在@Before带注释的Junit方法

中设置属性

答案 1 :(得分:0)

有一个扩展BaseDbTestCase的具体类,并将@ContextConfiguration放在具体类中。让你的其他测试类扩展这个具体的类。