困っていること
前回の質問から引き続きMockの挙動で悩んでいます。
あるプログラムからスタティッククラスをコールしています。
java
1public static AaaDao { 2 private static PreparedStatement ps = null; 3 -----中略------- 4 public int daoAccsess() { 5 String sql = "UPDATE AAA_TABLE SET A = ? WHERE B = ?"; 6 -------DB接続等省略----------- 7 ps = conn.prepareStatement(sql); 8 int i = ps.executeUpdate(); 9 } 10 ------- 中略 ----------- 11 return i; 12}
上記のようなコードがあったとします。
これをDB周りの部分をMock化してUTを行いたいのですが、上手く行きません。
テストコードは以下の通りです。
java
1@Test 2public void test001() { 3 @Test 4 public void test001(@Mocked PreparedStatement ps) throws SQLException { 5 6 new Expectations() {{ 7 @SuppressWarnings("unused") 8 int count = ps.executeUpdate(); 9 result = 0; 10 }}; 11 res = AaaDao.daoAccess(); 12 assertEquals(0,res); 13} 14
上記コードで実行すると下記のようなスタックとレースが出ます。
console
1mockit.internal.MissingInvocation: Missing 1 invocation to: 2java.sql.PreparedStatement#executeUpdate() 3 on mock instance: $Impl_PreparedStatement@174d20a 4 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 5 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 6 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 7 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 8 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 9 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 10Caused by: Missing invocations 11 at jp.co.zurich.sevenps.test.SevenpsLogDAOTest$1.<init>(SevenpsLogDAOTest.java:52) 12 at jp.co.zurich.sevenps.test.SevenpsLogDAOTest.test001(SevenpsLogDAOTest.java:50) 13 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 14 at java.lang.reflect.Method.invoke(Method.java:498) 15 ... 6 more 16
Expectations()で挙動を登録しているつもりなのですが、正規のルートが走ってしまい、期待した値が得られません。
staticメソッドの中身をモック化は出来ないのでしょうか?
質問ばかりで申し訳ありませんがお知恵を拝借出来たら幸いです。