Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I understand the question well, the class under test is <em>UnitOfMeasureRowMapper</em>. If this is true, then you don't want to mock it in your test, otherwise you are testing a mock! What is under test in your JUnit, is the behavior of <em>UnitOfMeasureRowMapper#mapRow</em> when <em>ResultSet</em> you give it throws a SQLException during the execution of the method. Then you want this method to return null. I would write it like this:</p> <pre><code>private ResultSet mockResultSet; private RowMapper&lt;UnitOfMeasure&gt; rowMapper = new UnitOfMeasureRowMapper(); public void setUp() throws Exception { mockResultSet = mock(ResultSet.class); } @Test public void mapRow_SHOULD_return_null_WHEN_resultSet_throws_a_SQLException() { when(mockResultSet.getInt(anyString()).thenThrow(new SQLException()); assertThat(mockRowMapper.mapRow(mockResultSet, 1), nullValue()); } </code></pre> <p>As suggested Samuel in his answer, you may set one of the method of the result set you use to throw a SQLException, and then check in your JUnit that the mapRow method returns null as expected. Here you are not testing the behavior of the result set, so its fine to mock it to achieve a behavior it would normally have under some circunstancies that would be painful to obtain otherwise. Mocking the result set behavior lets you focus on testing the RowMapper behavior.</p> <p>You are testing the <em>UnitOfMeasureRowMapper</em> that implements <em>RowMapper</em>. So have a <em>rowMapper</em> property in your JUnit, and I prefer to see it through its interface. I like to brutally call the constructor of <em>UnitOfMeasureRowMapper</em> because I want to keep my JUnit as simple as they can be.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload