How to mock JdbcTemplate.queryForObject() method
Asked Answered
R

5

6

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.

Renovate answered 11/12, 2012 at 10:51 Comment(0)
I
3

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);
Irisation answered 11/12, 2012 at 12:19 Comment(0)
R
12
@Mock
JdbcTemplate jdbctemplate;

@Test
public void testRun(){
when(jdbctemplate.queryForObject(anyString(),eq(String.class),anyObject()).thenReturn("data");
}
Renovate answered 11/12, 2012 at 12:20 Comment(1)
You have to add MockitoAnnotations.initMocks(this) in the before method to make this work with the Mock annotation.Fullrigged
I
3

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);
Irisation answered 11/12, 2012 at 12:19 Comment(0)
P
2

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.

Prakash answered 11/12, 2012 at 15:59 Comment(1)
Any idea how would a code for jdbcTemplate.update(query, param) look like. I've asked a question here related to mocking jdbcTemplate.update here too.Smallman
D
2

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

Diggs answered 27/1, 2015 at 21:4 Comment(0)
E
0

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> 
Eligible answered 22/12, 2020 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.