Spring jdbcTemplate unit testing

57,868

Solution 1

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<String> userNames = new ArrayList<String>();
    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.

Solution 2

You need to use Spring Test to do this. Take a look a the documentation:

http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/testing.html

You need to create a test using @RunWith and use your spring conf with @ContextConfiguration:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-config.xml")
public class SpringAppTests {
    @Autowired
    private HelloService helloService;

    @Test
    public void testSayHello() {
        Assert.assertEquals("Hello world!", helloService.sayHello());
    }
}

Here you have a little explanation from the documentation:

@Runwith

@Runwith(SpringJUnit4ClassRunner.class), developers can implement standard JUnit 4.4 unit and integration tests and simultaneously reap the benefits of the TestContext framework such as support for loading application contexts, dependency injection of test instances, transactional test method execution, etc.

@ContextConfiguration

@ContextConfiguration Defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests. Specifically, @ContextConfiguration declares either the application context resource locations or the annotated classes that will be used to load the context. Hope to help

Hope to help

Share:
57,868
Mat
Author by

Mat

Updated on July 22, 2022

Comments

  • Mat
    Mat almost 2 years

    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 getUserNames(final String userName {
      List<String> results = new LinkedList<String>();
       results =  service.getJdbcTemplate().query("SELECT USERNAME FROM USERNAMES WHERE NAME = ?", new RowMapper<String>() {
          @Override
          public String mapRow(ResultSet rs, int rowNum) throws SQLException {
              return new String(rs.getString("USERNAME");
          }
       }
    
       return results.get(0);      
       },userName)
    

    Does anyone have any suggestions on how I might achieve this using JUnit and Mockito?

    Thank you very much in advance!