bodybuilder2 / チュートリアル / オブジェクトを再帰的に表示・検査する


[ bodybuilder2 ]

ソース

public class SampleTest extends TestCase {

  public static void main(String[] args) {
    SampleTest test = new SampleTest();

    try {
      test.testSample();
    } catch (Throwable e) {
      e.printStackTrace(System.out);
    }
  }

  public void testSample() {
    AsserterFactory.register(new BeanAsserter(Foo.class));
    Asserter asserter = AsserterFactory.getAsserter();
    InspectorFactory.register(new BeanInspector(Foo.class));
    Inspector inspector = InspectorFactory.getInspector();

    List expected = new ArrayList();
    {
      expected.add("じゅげむじゅげむ");
      expected.add(new Integer(999));
      Map map = new HashMap();
      expected.add(map);
      map.put("文字列", "ごこうのすりきれ");
      map.put("数値", new Integer(999));
      Object[] ary = new Object[3];
      map.put("配列", ary);
      ary[0] = "ぱいぽぱいぽ";
      ary[1] = "しゅーりんがん";
      Set set = new LinkedHashSet();
      ary[2] = set;
      set.add("あいうえお");
      set.add("かきくけこ");
      Foo foo = new Foo();
      set.add(foo);
      foo.setAge(10);
      foo.setName("山田太郎");
    }

    List actual = new ArrayList();
    {
      actual.add("じゅげむじゅげむ");
      actual.add(new Integer(999));
      Map map = new HashMap();
      actual.add(map);
      map.put("文字列", "ごこうのすりきれ");
      map.put("数値", new Integer(999));
      Object[] ary = new Object[3];
      map.put("配列", ary);
      ary[0] = "ぱいぽぱいぽ";
      ary[1] = "シューリンガン";
      Set set = new LinkedHashSet();
      ary[2] = set;
      set.add("あいうえお");
      set.add("かきくけこ");
      Foo foo = new Foo();
      set.add(foo);
      foo.setAge(10);
      foo.setName("山田太郎");
    }

    System.out.println("# expected ############################");
    inspector.inspect(expected);

    System.out.println("\n# actual ##############################");
    inspector.inspect(actual);

    System.out.println("\n# assertion ###########################");
    asserter.assertEquals(expected, actual);
  }

}

結果

# expected ############################
ArrayList(3) {
  [0]=>
  String(8) "じゅげむじゅげむ"
  [1]=>
  Integer "999"
  [2]=>
  HashMap(3) {
    ["配列"]=>
    Object[](3) {
      [0]=>
      String(6) "ぱいぽぱいぽ"
      [1]=>
      String(7) "しゅーりんがん"
      [2]=>
      LinkedHashSet(3) {
        [0]=>
        String(5) "あいうえお"
        [1]=>
        String(5) "かきくけこ"
        [2]=>
        Foo {
          ["age"]=>
          Integer "10"
          ["name"]=>
          String(4) "山田太郎"
        }
      }
    }
    ["数値"]=>
    Integer "999"
    ["文字列"]=>
    String(8) "ごこうのすりきれ"
  }
}

# actual ##############################
ArrayList(3) {
  [0]=>
  String(8) "じゅげむじゅげむ"
  [1]=>
  Integer "999"
  [2]=>
  HashMap(3) {
    ["配列"]=>
    Object[](3) {
      [0]=>
      String(6) "ぱいぽぱいぽ"
      [1]=>
      String(7) "シューリンガン"
      [2]=>
      LinkedHashSet(3) {
        [0]=>
        String(5) "あいうえお"
        [1]=>
        String(5) "かきくけこ"
        [2]=>
        Foo {
          ["age"]=>
          Integer "10"
          ["name"]=>
          String(4) "山田太郎"
        }
      }
    }
    ["数値"]=>
    Integer "999"
    ["文字列"]=>
    String(8) "ごこうのすりきれ"
  }
}

# assertion ###########################
junit.framework.AssertionFailedError: objects differ expected:<しゅーりんがん> but was:<シューリンガン>
ArrayList(3) {
  [2]=>
  HashMap(3) {
    ["配列"]=>
    Object[](3) {
      [1]=>
      String(7)

	at rubbish.bb2.asserter.impl.AbstractAsserter.rethrow(AbstractAsserter.java:48)
	at rubbish.bb2.asserter.impl.AbstractAsserter.rethrow(AbstractAsserter.java:31)
	at rubbish.bb2.asserter.impl.DefaultAsserter.assertEquals(DefaultAsserter.java:23)
	at rubbish.bb2.asserter.impl.ArrayAsserter.assertEquals(ArrayAsserter.java:24)
	at rubbish.bb2.asserter.impl.MapAsserter.assertEquals(MapAsserter.java:28)
	at rubbish.bb2.asserter.impl.CollectionAsserter.assertEquals(CollectionAsserter.java:29)
	at rubbish.bb2.asserter.AsserterFactory$1.assertEquals(AsserterFactory.java:30)
	at rubbish.bb2.asserter.impl.AbstractAsserter.assertEquals(AbstractAsserter.java:21)
	at sample.SampleTest.testSample(SampleTest.java:89)
	at sample.SampleTest.main(SampleTest.java:26)