This example shows how to write junit to test spring ResultSetExtractor functionality while mocking JdbcTemplate with Mockito.
This also increases code coverage of ResultSetExtractor code.
Below is the DAO class that returns Map of employees with ResultSetExtractor using Lambdas
Spring JdbcTemplate ResultSetExtractor Example with Lambdas
package com.kswaughs.dao; import java.sql.ResultSet; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import com.kswaughs.beans.Employee; @Repository public class EmployeeDAO { @Autowired private JdbcTemplate jdbcTemplate; public Map<Integer, Employee> getEmployeeMap() { return jdbcTemplate.query("SELECT ID, NAME, STATUS FROM EMPLOYEE", (ResultSet rs) -> { Map<Integer, Employee> employeeMap = new HashMap<>(); while (rs.next()) { Employee employee = new Employee(); employee.setId(rs.getInt("ID")); employee.setName(rs.getString("NAME")); employee.setStatus(rs.getBoolean("STATUS")); employeeMap.put(employee.getId(), employee); } return employeeMap; }); } }
Below is the Junit test class to test ResultSetExtractor code by mocking JdbcTemplate to return two rows.
Spring JdbcTemplate ResultSetExtractor Junit test example
package com.kswaughs.dao; import static org.mockito.Mockito.when; import java.sql.ResultSet; import java.util.Map; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentMatchers; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.ResultSetExtractor; import com.kswaughs.beans.Employee; @RunWith(MockitoJUnitRunner.class) public class EmployeeDAOTest { @Mock private JdbcTemplate jdbcTemplate; @InjectMocks private EmployeeDAO employeeDAO; @SuppressWarnings("unchecked") @Test public void testGetEmployeeMap() { Mockito.when(jdbcTemplate.query( ArgumentMatchers.anyString(), ArgumentMatchers.any(ResultSetExtractor.class))) .thenAnswer((invocation) -> { ResultSetExtractor<Map<Integer, Employee>> resultSetExtractor = (ResultSetExtractor<Map<Integer, Employee>>) invocation.getArgument(1); ResultSet rs = Mockito.mock(ResultSet.class); // two times it returns true and third time returns false. when(rs.next()).thenReturn(true, true, false); // Mock ResultSet to return two rows. Mockito.when(rs.getInt(ArgumentMatchers.eq("ID"))) .thenReturn(506, 400); Mockito.when(rs.getString(ArgumentMatchers.eq("NAME"))) .thenReturn("Jim Carrey", "John Travolta"); Mockito.when(rs.getBoolean(ArgumentMatchers.eq("STATUS"))) .thenReturn(true, false); return resultSetExtractor.extractData(rs); }); Map<Integer, Employee> employeeMap = employeeDAO.getEmployeeMap(); // Assert First Row assertFirstUser(employeeMap.get(506)); // Assert Second Row assertSecondUser(employeeMap.get(400)); } public void assertFirstUser(Employee employee) { Assert.assertEquals(Integer.valueOf(506), employee.getId()); Assert.assertEquals("Jim Carrey", employee.getName()); Assert.assertTrue(employee.isStatus()); } public void assertSecondUser(Employee employee) { Assert.assertEquals(Integer.valueOf(400), employee.getId()); Assert.assertEquals("John Travolta", employee.getName()); Assert.assertFalse(employee.isStatus()); } }
Not working
ReplyDeleteNice, thank you
ReplyDeleteNot working
ReplyDeletePerfect, fixed my issues with code coverage.
ReplyDeletehttps://bayanlarsitesi.com/
ReplyDeleteYenibosna
Anadolu Kavağı
İçerenköy
Yeşilköy
V8Fİ
Malatya
ReplyDeleteKırıkkale
Aksaray
Bitlis
Manisa
AGİX3R
Afyon
ReplyDeleteAntalya
Erzurum
Mersin
izmir
DCXOA
Tokat Lojistik
ReplyDeleteKonya Lojistik
Mersin Lojistik
Karabük Lojistik
Samsun Lojistik
T11F4T
aydın evden eve nakliyat
ReplyDeletebursa evden eve nakliyat
trabzon evden eve nakliyat
bilecik evden eve nakliyat
antep evden eve nakliyat
ASTBW
6D5ED
ReplyDeletebinance komisyon indirimi
2380D
ReplyDeletegate io
binance referans kimliği
poloniex
binance referans
gate io
canlı sohbet ucretsiz
bitcoin seans saatleri
paribu
binance referans kod
E0E7D3D7E4
ReplyDeleteinstagram türk beğeni
D01B078CF1
ReplyDeleteinstagram ucuz beğeni satın al