My method looks like this:
public class Decompile extends JdbcDaoSupport
public void getRunner(){
String val = this.getJdbcTemplate().queryForObject(sql,String.class, new Object[]{1001});
}
}
Please suggest how I would mock this.
My method looks like this:
public class Decompile extends JdbcDaoSupport
public void getRunner(){
String val = this.getJdbcTemplate().queryForObject(sql,String.class, new Object[]{1001});
}
}
Please suggest how I would mock this.
an EasyMock-3.0 example
String sql = "select * from t1";
Object[] params = new Object[] { 1001 };
JdbcTemplate t = EasyMock.createMock(JdbcTemplate.class);
EasyMock.expect(
t.queryForObject(sql, String.class, params)).andReturn("res");
EasyMock.replay(t);
@Mock
JdbcTemplate jdbctemplate;
@Test
public void testRun(){
when(jdbctemplate.queryForObject(anyString(),eq(String.class),anyObject()).thenReturn("data");
}
an EasyMock-3.0 example
String sql = "select * from t1";
Object[] params = new Object[] { 1001 };
JdbcTemplate t = EasyMock.createMock(JdbcTemplate.class);
EasyMock.expect(
t.queryForObject(sql, String.class, params)).andReturn("res");
EasyMock.replay(t);
Use JMockit, the code will be like this:
@Mocked
JdbcTemplate jdbcTemplate
new Expectations() {
jdbcTemplate.queryForObject(sql,String.class, new Object[]{1001});
result = "result you want";
}
More information about JMockit is here.
jdbcTemplate.update(query, param)
look like. I've asked a question here related to mocking jdbcTemplate.update here too. –
Smallman Using Mockito, you can also mock the queryForObject(..) method as follows :
@Mock
JdbcTemplate jdbctemplate;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testRun(){
when(jdbctemplate.queryForObject(eq("input string"), refEq(new Object[]{1001}), eq(String.class))).thenReturn("data");
}
Some additional source of information - http://sourcesnippets.blogspot.com/2013/06/jdbc-dao-unit-test-using-mockito.html
Following is the working code i used while testing similar method
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.jdbc.core.JdbcTemplate;
@RunWith(MockitoJUnitRunner.class)
public class DecompileTest {
@Mock/* works fine with autowired dependency too */
JdbcTemplate jdbcTemplate;
@InjectMocks/* for the claa that we are mocking */
Decompile testclass;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testgetRunner() {
Mockito.lenient().when(jdbcTemplate.queryForObject("select * from .. ",String.class, new Object[] { 1001 } )).thenReturn("resultval");
testclass.getRunner();
}
}
mvn package used as following ( compatible with spring version > 2 /* tested */ )
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
© 2022 - 2024 — McMap. All rights reserved.