2017-07-18 96 views
0

このコードは、以下の例外をスローしないようにどのように変更する必要がありますか?Mockito。引数値が取得されませんでした

ArgumentCaptor<Date> argument = forClass(Date.class); 
verify(ps, times(0)).setDate(anyInt(), argument.capture()); 

typeHandler.setNonNullParameter(ps, 1, "20170120", DATE); 

assertEquals(new Date(2017, 01, 20), argument.getValue()); 

もっとコード:

import org.apache.ibatis.type.BaseTypeHandler; 
import org.apache.ibatis.type.JdbcType; 
import org.joda.time.LocalDate; 
import org.joda.time.format.DateTimeFormat; 
import org.joda.time.format.DateTimeFormatter; 

import java.sql.*; 

public class DateStringTypeHandler extends BaseTypeHandler<String> { 

    private static final DateTimeFormatter YYYY_MM_DD = DateTimeFormat.forPattern("yyyyMMdd"); 

    @Override 
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { 
     LocalDate localDate = YYYY_MM_DD.parseLocalDate(parameter); 
     ps.setDate(i, new Date(localDate.toDateTimeAtStartOfDay().getMillis())); 
    } 
} 

@RunWith(MockitoJUnitRunner.class) 
public class DateStringTypeHandlerTest { 

    @Mock 
    private PreparedStatement ps; 
    private DateStringTypeHandler typeHandler; 

    @Before 
    public void before() { 
     typeHandler = new DateStringTypeHandler(); 
    } 

    @Test 
    public void testSetNonNullParameterPreparedStatementIntStringJdbcType() throws SQLException { 
     ArgumentCaptor<Date> argument = forClass(Date.class); 
     verify(ps, times(0)).setDate(anyInt(), argument.capture()); 

     typeHandler.setNonNullParameter(ps, 1, "20170120", DATE); 

     assertEquals(new Date(2017, 01, 20), argument.getValue()); 
    } 
}  

verifyは例外をスロー:

org.mockito.exceptions.base.MockitoException: 
No argument value was captured! 
You might have forgotten to use argument.capture() in verify()... 
...or you used capture() in stubbing but stubbed method was not called. 
Be aware that it is recommended to use capture() only with verify() 

Examples of correct argument capturing: 
    ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class); 
    verify(mock).doSomething(argument.capture()); 
    assertEquals("John", argument.getValue().getName()); 
+0

何か問題が発生したことを確認したばかりの場合、どうやってその議論が取り込まれると思いますか?回避策では – khelwood

+0

をゼロ回削除する必要がありますが、削除すると別の例外がスローされます。何かが間違って使用されますが、私は何をどのように使用するかを理解することはできません。 –

+0

しかし、このメソッドは呼び出されていません。それは 'verify(...、times(0))'が確認しているものです。だからあなたが得るためのキャプチャされた議論はありません。 – khelwood

答えて

3

あなたは最初のテストの下のクラスのメソッドを呼び出す必要があります。そして、あなたはキャプターを使用して確認してください。また

@Test 
    public void testSetNonNullParameterPreparedStatementIntStringJdbcType() throws SQLException { 
     // Arrange 
     ArgumentCaptor<Date> argument = forClass(Date.class); 

     // Act 
     typeHandler.setNonNullParameter(ps, 1, "20170120", DATE); 

     // Assert    
     verify(ps).setDate(anyInt(), argument.capture());  
     assertEquals(new Date(2017, 01, 20), argument.getValue()); 
    } 

今、あなたはおそらくtimes(..)引数を必要としません。

+0

ありがとう、私のために働く。議論を最初に初期化しなければならないと思った(exaples promote)。 –

関連する問題