0

I have a straightforward method to test. I tested all my other methods that return a custom object and they're working fine.


@Autowired Dao dao;

public ResultSetList getResultSetList(String f, String t) {
    ResultSetList res = dao.getResultst(f, t);
    return res;
}

@Mock
private ResultSet rs;

@Test
public void testGetResultSetList() {
    ResultSetList rsl = new ResultSetList(rs, 0, 0, true); // npe here

    Mockito.when(dao.getResults(any(), any())).thenReturn(rsl);

    ResultSetList rsFinal = service.getResultSetList("", "");

    Assertions.assertThat(rsFinal).isNotNull();
}
Juliano Macedo
  • 668
  • 8
  • 21
Spider
  • 431
  • 7
  • 21
  • 1
    You don't show any code that would have initialized `rs`. – chrylis -cautiouslyoptimistic- Aug 26 '20 at 22:40
  • Wouldn't @Mock initialize it? – Spider Aug 26 '20 at 22:41
  • 1
    Annotations aren't magic. Do you have some sort of additional rule attached to this class that _would_ initialize it? (It's for just this kind of thing that I personally prefer to de-magic and just use `mock(ResultSet.class)`.) – chrylis -cautiouslyoptimistic- Aug 26 '20 at 22:43
  • I thought those two things did the same thing? Using ```Mockito.mock(ResultSet.class)``` didn't work for me. – Spider Aug 26 '20 at 22:54
  • Is there a workaround so I can just initialize ResultSetList? Because that's really what I want to do at the end of the day. – Spider Aug 26 '20 at 23:04
  • 1. You need to initialize your mocks. See [this answer](https://stackoverflow.com/a/23563816/4494577). 2. Post the stacktrace. 3. `Using Mockito.mock(ResultSet.class) didn't work for me.` - what does it mean? 4. As you wrote - why not mock `ResultSetList`? – jannis Aug 26 '20 at 23:15
  • Because ResultSetList is a final class – Spider Aug 26 '20 at 23:23
  • I realized you can change config to enable Mockito to test final classes. My bad, it works now – Spider Aug 26 '20 at 23:30

0 Answers0