1

ユニットテストにUnitilswithSpringを使用しています。プロパティファイルを使用して、データソースでSpringを構成しました。

私の質問は、Unitilsに同じデータソースまたは同じプロパティをどのように使用できるかということです。

Unitilsは、url、user、password、driverなどのデータベース構成パラメーターを持つクラスパスunitils.properties内のファイルを想定しています。

以下のSpring構成で使用されるプロパティを使用してUnitilsを構成しようとしましたが、機能しません。

database.driverClassName=${jdbc.driver.class}

ありがとう、アディ

4

3 に答える 3

1

考えられる解決策の 1 つ... その逆ではなく、Spring 構成で unitils.properties からデータソース パラメータを読み取ることができます。おそらく理想的ではありません。

unitils は内部で spring を使用していると思います@SpringApplicationContext。起動時に unitils によって設定されたデータソース Bean の名前を把握できた場合は、コンテキストでそれをオーバーライドできます (他の Spring Bean が作成される前に unitils データソース Bean が作成されると仮定すると、これは真である場合とそうでない場合があります)。

例えば

@SpringApplicationContext({"correctDataSourceContext.xml"})

編集: 間違いなく機能する別のオプション: https://stackoverflow.com/a/6561782/411229 基本的に Unitils を自分でインスタンス化し、プロパティを手動で設定します。

于 2012-02-21T19:19:53.107 に答える
1

私は別のアプローチを使用しましたが、ライアンの答えも正しくて役に立ちます。

クラスを拡張して、PropertiesDataSourceFactory次のようにメソッドをオーバーライドしました。

public class UnitilsDataSourceFactory extends PropertiesDataSourceFactory {

    @Override
    public void init(Properties configuration) {
        try {
            String[] configFiles = new String[] { "applicationContext-test.xml" };
            BeanFactory factory = new ClassPathXmlApplicationContext(configFiles);

            SystemPropertiesReader systemPropertiesReader = (SystemPropertiesReader) factory.getBean("systemPropertiesReader");
            Properties loadProperties = systemPropertiesReader.loadProperties();

            super.init(loadProperties);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public DataSource createDataSource() {
        DataSource dataSource = super.createDataSource();
        return dataSource;
    }

}

また、SystemPropertiesReader を次のように記述しました。

public class SystemPropertiesReader {

    private Collection<Resource> resources;

    public void setResources(final Collection<Resource> resources) {
        this.resources = resources;
    }

    public void setResource(final Resource resource) {
        resources = Collections.singleton(resource);
    }

    @PostConstruct
    public Properties loadProperties() throws Exception {
        final Properties systemProperties = System.getProperties();
        for (final Resource resource : resources) {
            final InputStream inputStream = resource.getInputStream();
            try {
                systemProperties.load(inputStream);
            } finally {
                //
            }
        }

        return systemProperties;
    }

}

プロパティファイルでBeanを追加しました:

<bean id="systemPropertiesReader" class="uk.co.friendslife.eventmanager.domain.dao.SystemPropertiesReader">
            <property name="resource">
                <value>classpath:/META-INF/em/config/eventmanager_${database_name_lower}.properties</value>
            </property>
</bean>

以下を unitils.properties に追加します。

org.unitils.database.config.DataSourceFactory.implClassName=x.y.UnitilsDataSourceFactory
于 2012-02-22T18:46:00.487 に答える
0

いくつかのアイデアを追加したいだけで、それがベストプラクティスであるかどうかわからないので、何か問題がある場合は修正してください。

  • MYPROJECT
    - src
    --TestPackage
    ---BaseServiceTest.class
    ---BlogspotServiceTest.class
    --hibernate.cfg.xml
    - web
    --WEB-INF
    ---blogspot-servlet-test.xml
    ---jdbc-test.properties

私の場合、blogspot-servlet-test.xml を使用してデータソースを呼び出しまたは作成しました

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

     .... some bean configuration

    <bean id="propertyConfigurer" 
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="file:web/WEB-INF/jdbc.properties"/>


    <bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.databaseurl}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}"/>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
     </bean>

     <!-- DAO'S -->
     <bean id="blogspotDAO" class="package.BlogspotDAOImpl"/>

     <!-- SERVICES -->
     <bean id="blogspotService" class="package.BlogspotServiceImpl"/>

     <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
     </bean>


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

私の jdbc-test.properties ファイル

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQL5Dialect
jdbc.databaseurl=jdbc:mysql://127.0.0.1:3306/dbspringminiblogtest
jdbc.username=root
jdbc.password=

hibernate.cfg.xml の場合

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd//hibernate-configuration-3.0.dtd">

    <hibernate-configuration>
        <session-factory>
            <mapping class="somePackage.entity.Author"/>
            <!-- Other Entity Class to be mapped -->

        </session-factory>
    </hibernate-configuration>

そして、複数の @SpringApplicationContext アノテーションの作成を減らすために BaseClass を作成しました。これは、他のクラスのテストに必要な共通の構成を構成するためにも使用され、拡張するだけです。

@SpringApplicationContext({"file:web/WEB-INF/blogspot-servlet-test.xml"})
public class BaseServiceTest extends UnitilsJUnit4 {
}

@SpringApplicationContext を使用して、データソースとその他の Bean 構成を BaseClass にロードしました。これが実装方法です。

以下 : 詳細については、 Spring-Uni​​tils チュートリアル を参照してください

public class BlogspotServiceTest extends BaseServiceTest{

    @Mock
    @InjectInto(property = "blogspotDAO")
    @SpringBean("blogspotDAO")
    private BlogspotDAO blogspotDAOMock;

    @TestedObject
    @SpringBean("blogspotService")
    private BlogspotService blogspotServiceMock;

    @Test
    public void testAddBlogSpot() {
        assertNotNull("BlogspotService Not null",blogspotServiceMock);
    }
}

注:プログラムを実行できるように、TestPackage 内に unitils.properties と unitils-local.properties を作成してください。

@SpringBean の説明とその他の注釈については、以下をお読みください。

Unitils-EasyMock

于 2014-09-30T06:42:17.793 に答える