Issue with stubbing/mocking the method with makes a DB call

∥☆過路亽.° 提交于 2021-02-10 06:22:08

问题


I am having issue with Mocking a JDBC call using the MockitoJUnitRunner. Somehow Mockito is not mocking the actual call even though I have below subbing line into the test class.

when(readOnlyJdbcTemplate.query(anyString(), any(Object[].class), any(int[].class), any(FeatureCollectionResponseExtractor.class))).thenReturn(actual);

Very similar mocking is working in another class for very similar type of method. The only difference between them is my other class does have 3 parameters instead of 4 parameters. Below is the code which is actually mocking successfully for different class.

when(readOnlyJdbcTemplate.query(anyString(), any(Object[].class), any(FeaturesResultExtractor.class))).thenReturn(actual);

Below is my actual code.

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.inject.Inject;
import javax.inject.Named;
import java.net.HttpURLConnection;
import java.sql.Types;

import static com.accounts.features.utils.Constants.INTERNAL_SERVER_ERROR;

@Profile
@Log
@Named("featureLibraryDao")
public class FeatureLibraryDaoImpl implements FeatureLibraryDao {

    private static final Logger LOGGER = LogManager.getLogger(FeatureLibraryDaoImpl.class);

    @Value("${feature.library.function.sql.query}")
    private String sqlSelectQuery;

    @Inject
    @Named("readOnlyJdbcTemplate")
    private JdbcTemplate readOnlyJdbcTemplate;

    @Override
    public FeatureCollectionDTO getFeaturesData(FeatureRequest request) {
        try {
            int[] argTypes = new int[] { Types.BIGINT, Types.VARCHAR, Types.SMALLINT};
            return readOnlyJdbcTemplate.query(sqlSelectQuery, new Object[] {
                        Long.parseLong(request.getAccountId()), request.getRequestedFeatures(), request.getApplicationSuffix()
                    }, argTypes,
                    new FeatureCollectionResponseExtractor(request));
        } catch (CustomException cbe) {
            throw cbe;
        } catch (Exception ex) {
            LOGGER.error("getFeaturesData method failed with error message:{}", ex.getMessage(), ex);

            CustomErrorCode error = new CustomErrorCode(INTERNAL_SERVER_ERROR);
            error.setDeveloperText(ex.getMessage());
            throw new CustomSystemException(error, HttpURLConnection.HTTP_INTERNAL_ERROR);
        }
    }

}

and below is my test class.

@RunWith(MockitoJUnitRunner.class)
public class FeatureLibraryDaoImplTest {

    @InjectMocks
    private FeatureLibraryDaoImpl dao;

    @Mock
    private JdbcTemplate readOnlyJdbcTemplate;

    private List<String> features = Arrays.asList("excl_clsd_ind_only", "excl_chrgoff_ind_only", "excl_dsput_ind_only");

    @Test
    public void getFeaturesDataWhenSuccess() {
        //given
        FeatureRequest request = getFeatureRequest();
        FeatureCollectionDTO actual = new FeatureCollectionDTO(features);

        when(readOnlyJdbcTemplate.query(anyString(), any(Object[].class), any(int[].class), any(FeatureCollectionResponseExtractor.class))).thenReturn(actual);

        //when
        FeatureCollectionDTO dto = dao.getFeaturesData(request);

        //then
        assertThat(dto, notNullValue());
    }
}

Any suggestion about what is wrong here? Is there any issue with any(int[].class) ?


回答1:


I do see you are not passing the sql query sqlSelectQuery value during the test case, But during mock you specified anyString() so it must be some value but not null. Since you are using spring project, you can use ReflectionTestUtils to set the field value for object

@Before
public void setUp() {
    ReflectionTestUtils.setField(dao, "sqlSelectQuery", "query");

}



回答2:


Hey Guys thanks much for all your suggestions. So I found that Test code is perfectly fine. Some how @Value tag wasn't injecting the actual value the sqlSelectQuery in the main code file. @Value("${feature.library.function.sql.query}") private String sqlSelectQuery;

Instead of that I changed code to private String sqlSelectQuery = "${feature.library.function.sql.query}" and all test cases are passing.

Somehow sqlSelectQuery was't getting the value and hence Mockito wasn't mocking the actual method call. I am yet reviewing why @value is not working as it should be.



来源:https://stackoverflow.com/questions/58438839/issue-with-stubbing-mocking-the-method-with-makes-a-db-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!