I am new to Spring and only somewhat experienced with JUnit and Mockito
I have the following method which requires a unit test
public static String
If you want to do a pure unit test then for the line
service.getJdbcTemplate().query("....");
You will need to mock the Service, then the service.getJdbcTemplate() method to return a mock JdbcTemplate object, then mock the query method of mocked JdbcTemplate to return the List you need. Something like this:
@Mock
Service service;
@Mock
JdbcTemplate jdbcTemplate;
@Test
public void testGetUserNames() {
List userNames = new ArrayList();
userNames.add("bob");
when(service.getJdbcTemplate()).thenReturn(jdbcTemplate);
when(jdbcTemplate.query(anyString(), anyObject()).thenReturn(userNames);
String retVal = Class.getUserNames("test");
assertEquals("bob", retVal);
}
The above doesn't require any sort of Spring support. If you were doing an Integration Test where you actually wanted to test that data was being pulled from a DB properly, then you would probably want to use the Spring Test Runner.