これは何?

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <artifactId>SampleEar</artifactId>
        <groupId>com.mycompany</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.mycompany</groupId>
    <artifactId>SampleEar-ejb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>ejb</packaging>

    <name>SampleEar-ejb</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <netbeans.hint.deploy.server>gfv3ee6</netbeans.hint.deploy.server>
        <jmockit.version>1.1</jmockit.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.3.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.3.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.9.1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>${jmockit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.dbunit</groupId>
            <artifactId>dbunit</artifactId>
            <version>2.4.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.5.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.2-FINAL</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>SampleEar-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin> 
                <artifactId>maven-surefire-plugin</artifactId> 
                <version>2.14</version>
                <configuration>
                    <argLine>
-javaagent:"${settings.localRepository}"/com/googlecode/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
                    </argLine>
                </configuration> 
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url>
            <id>eclipselink</id>
            <layout>default</layout>
            <name>Repository for library EclipseLink (JPA 2.0)</name>
        </repository>
    </repositories>
</project>

テスト対象

テストプログラム1 (dbunit)

package com.mycompany.test.biz;

import com.mycompany.biz.OrderService;
import com.mycompany.entity.OrderTable;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import mockit.Deencapsulation;
import org.dbunit.Assertion;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.excel.XlsDataSet;
import org.dbunit.dataset.filter.DefaultColumnFilter;
import org.dbunit.operation.DatabaseOperation;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.equalTo;

/**
 *
 * @author atsushi
 */
public class OrderServiceTest {
    private static EntityManagerFactory emf;
    private static EntityManager em;
    private static OrderService target = new OrderService();

    @BeforeClass
    public static void setUpClass() {
        emf = Persistence.createEntityManagerFactory("warehouseTestPU");
        em = emf.createEntityManager();
        
        
        // Injection
        Deencapsulation.setField(target, "em", em);
    }

    @AfterClass
    public static void tearDownClass() {
        em.close();
        emf.close();
    }

    @Test
    public void test() throws Exception {
        loadTestDb("src/test/resources/OrderServiceTest_ini.xls");
        
        // ▽ Execute Target
        em.getTransaction().begin();

        OrderTable order = target.orderItem(1, 1, 20);
        
        em.getTransaction().commit();
        // △ Execute Target
        
        assertThat(1L, is(equalTo(order.getCustomerId().getId())));
        assertThat(1L, is(equalTo(order.getItemId().getId())));
        assertThat(80L, is(equalTo(order.getItemId().getStock())));
        assertThat(20L, is(equalTo(order.getAmount())));
        
        IDataSet actualTable = loadActualTable();
        IDataSet expectedTable = new XlsDataSet(new File("src/test/resources/OrderServiceTest_exp.xls"));
        
        // Filter shipdate
        ITable filteredTable = DefaultColumnFilter.includedColumnsTable(
            new SortedTable(
                 actualTable.getTable("order_table"), new String[]{"id"}), 
            expectedTable.getTableMetaData("order_table").getColumns());
        
        Assertion.assertEquals(expectedTable.getTable("order_table"), filteredTable);
    }
    
    private void loadTestDb(String excel) throws DatabaseUnitException, SQLException, IOException {
        em.getTransaction().begin();
        Connection jdbc = em.unwrap(java.sql.Connection.class);
        IDatabaseConnection conDbUnit = new DatabaseConnection(jdbc);
        IDataSet dataSet = new XlsDataSet(new File(excel));
        DatabaseOperation.CLEAN_INSERT.execute(conDbUnit,dataSet);
        em.getTransaction().commit();
    }
        
    private IDataSet loadActualTable() throws DatabaseUnitException, SQLException, IOException {
        em.getTransaction().begin();
        Connection jdbc = em.unwrap(java.sql.Connection.class);
        IDatabaseConnection conDbUnit = new DatabaseConnection(jdbc);
        
        IDataSet dataSet = conDbUnit.createDataSet(new String[]{"item_table", "customer_table", "order_table"});
        //XlsDataSet.write(dataSet, new FileOutputStream(new File("/tmp/result.xls")));
        em.getTransaction().commit();
        
        return dataSet;
    }
}

テストプログラム2 (jmockit)

package com.mycompany.test.biz;

import com.mycompany.biz.FactoryService;
import com.mycompany.biz.OrderService;
import com.mycompany.entity.OrderTable;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import mockit.Deencapsulation;
import mockit.Expectations;
import mockit.Mocked;
import org.apache.commons.lang.time.DateUtils;
import org.dbunit.DatabaseUnitException;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.excel.XlsDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author atsushi
 */
public class OrderServiceTest2 {
    private EntityManagerFactory emf;
    private EntityManager em;
    private OrderService target = new OrderService();
    
    /**
     * MockObject.
     * 注意 "private static" 不可
     */
    @Mocked
    private FactoryService mockFactory;

    @Before
    public void setUp() {
        emf = Persistence.createEntityManagerFactory("warehouseTestPU");
        em = emf.createEntityManager();
        
        // Injection
        Deencapsulation.setField(target, "em", em);
        Deencapsulation.setField(target, "factory", mockFactory);
    }

    @After
    public void tearDown() {
        em.close();
        emf.close();
    }

    @Test
    public void test() throws Exception {
        loadTestDb("src/test/resources/OrderServiceTest_ini.xls");
        
        new Expectations() {{            
            // mockFactory が 引数 (1L,20) で 1 回呼ばれる事を期待する。
            // 返値には、現在日の 2 日後を一律返す
            Date mockReturn = DateUtils.addDays(new Date(), 2);
            mockFactory.backorder(1L, 20); result=mockReturn; times=1;
        }};
        // ▽ Execute Target
        em.getTransaction().begin();

        OrderTable order = target.orderItem(1, 1, 120);
        
        em.getTransaction().commit();
        // △ Execute Target
        
        // 検証略
    }
    
    private void loadTestDb(String excel) throws DatabaseUnitException, SQLException, IOException {
        em.getTransaction().begin();
        Connection jdbc = em.unwrap(java.sql.Connection.class);
        IDatabaseConnection conDbUnit = new DatabaseConnection(jdbc);
        IDataSet dataSet = new XlsDataSet(new File(excel));
        DatabaseOperation.CLEAN_INSERT.execute(conDbUnit,dataSet);
        em.getTransaction().commit();
    }        
}


Java#Glassfish


添付ファイル: fileexcel4.png 429件 [詳細] fileexcel3.png 434件 [詳細] fileexcel2.png 429件 [詳細] fileexcel1.png 412件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2013-04-19 (金) 01:19:50 (1331d)
ISBN10
ISBN13
9784061426061