Quantcast
Channel: プログラム の個人的なメモ
Viewing all articles
Browse latest Browse all 860

【JUnit】 AssertJ-DB に関するあれこれ

$
0
0

はじめに

http://blogs.yahoo.co.jp/dk521123/36157721.html
で取り扱った AssertJ-DB だが、全然情報がないので
分かった事をこまめにメモる。

■ 日付について

http://joel-costigliola.github.io/assertj/assertj-db-concepts.html#datevalue-timevalue-datetimevalue
 * DateValue : 日付
 * TimeValue : 時間
 * DateTimeValue : 日時

サンプル

サンプルデータ:MySQL

CREATE TABLE IF NOT EXISTS `item` (
  `id` char(8) DEFAULT NULL,
  `name` varchar(100) DEFAULT NULL,
  `price` int(11) DEFAULT NULL,
  `releasedate` datetime DEFAULT NULL,
  `createdate` datetime DEFAULT current_timestamp,
  `updatedate` datetime DEFAULT current_timestamp on update current_timestamp
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Item.java

import java.sql.Date;
import java.util.Calendar;

public class Item {
  public String id;
  public String name;
  public Long price;
  public Calendar releaseDate;
  public Date createDate;
  public Date updateDate;
}

TargetSample3.java

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TargetSample3 {
  public static int insert(String id, String name, Integer price, Date releaseDate) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");

    int result = 0;
    try (
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledb", "root", "password");
        PreparedStatement preparedStatement = connection
            .prepareStatement("INSERT INTO item(id, name, price, releasedate) VALUES(?, ?, ?, ?)");) {
      preparedStatement.setString(1, id);
      preparedStatement.setString(2, name);
      preparedStatement.setInt(3, price);
      preparedStatement.setDate(4, releaseDate);
      result = preparedStatement.executeUpdate();
    } catch (SQLException ex) {
      ex.printStackTrace();
    }
    return result;
  }
}

TargetSample3Test.java

import static org.junit.Assert.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import static com.ninja_squad.dbsetup.Operations.*;
import static org.assertj.db.api.Assertions.assertThat;

import org.assertj.db.type.DateTimeValue;
import org.assertj.db.type.Source;
import org.assertj.db.type.Table;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.ninja_squad.dbsetup.DbSetup;
import com.ninja_squad.dbsetup.destination.Destination;
import com.ninja_squad.dbsetup.destination.DriverManagerDestination;
import com.ninja_squad.dbsetup.operation.Operation;

/**
 *
 */
public class TargetSample3Test {
  public static final Operation DELETE_ALL = deleteAllFrom("item");

  /**
   * @throws java.lang.Exception
   */
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
  }

  /**
   * @throws java.lang.Exception
   */
  @AfterClass
  public static void tearDownAfterClass() throws Exception {
  }

  /**
   * @throws java.lang.Exception
   */
  @Before
  public void setUp() throws Exception {
    Operation operation = sequenceOf(DELETE_ALL);

    Destination destination = new DriverManagerDestination("jdbc:mysql://localhost:3306/sampledb", "root", "password");
    DbSetup dbSetup = new DbSetup(destination, operation);
    dbSetup.launch();
  }

  /**
   * @throws java.lang.Exception
   */
  @After
  public void tearDown() throws Exception {
  }

  /**
   * INSERT Test.
   */
  @Test
  public void insertTest() {
    try {
      Calendar targetDate = Calendar.getInstance();
      System.out.println("Debug : " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss SSS").format(targetDate.getTime()));
      int result = TargetSample3.insert("X0001", "Apple", 129, targetDate);
      assertEquals(1, result);

      Source source = new Source("jdbc:mysql://localhost:3306/sampledb", "root", "password");
      Table table = new Table(source, "item");

      assertThat(table).hasNumberOfRows(1);
      assertThat(table).row(0).value("id").isEqualTo("X0001");
      assertThat(table).row(0).value("name").isEqualTo("Apple");
      assertThat(table).row(0).value("price").isEqualTo(129);
      assertThat(table).row(0).value("releasedate").isAfterOrEqualTo(new DateTimeValue(targetDate));
      assertThat(table).row(0).value("createdate").isAfterOrEqualTo(new DateTimeValue(targetDate));
      assertThat(table).row(0).value("updatedate").isAfterOrEqualTo(new DateTimeValue(targetDate));
    } catch (Exception ex) {
      ex.printStackTrace();
      fail("Exception...");
    }
  }
}


関連記事

DBにまつわる単体試験 [3] ~ AssertJ / AssertJ-DB ~

http://blogs.yahoo.co.jp/dk521123/36157721.html

Viewing all articles
Browse latest Browse all 860

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>