Search code examples
javajunit4spring-datajmockit

JMockit - java.lang.ClassCastException: java.lang.String cannot be cast to Organization


Organization is a POJO. Here is the test class:

@Tested
DAOImpl daoImpl;

    @Injectable
    JdbcTemplate mockJdbcTemplate;

    @Mocked
    DAO ssoDAO;

    @Before
    public void setup() {
        daoImpl = new DAOImpl();
        daoImpl.setJdbcTempate(mockJdbcTemplate);
    }

    @Test
    public void testAddOrg() {

        final String expectedId = "7c82facc";
        final String expectedOrg = "one";

        new Expectations() {{
            mockJdbcTemplate.queryForObject(DAOImpl.GET__KEY_FOR_ORG_IDS, (DAORowMapper) any, expectedId, expectedKeys);
            result = expectedKeys;
        }};

        Organization actualKey =daoImpl.addOrg(expectedId, expectedKeys);
        assertEquals(expectedKeys, actualKey);
    }

}

Here is the class under test: with the method I am testing for:

@Repository(value = "dao")
public class DAOImpl implements DAO {
    private JdbcTemplate jdbcTemplate;

    static final String GET_KEY_FOR_ORG_IDS = "select keys from table where id=?";

    static final String DATASOURCE_BEAN = "dataSource";

    @Autowired
    public void createTemplate(
            @Qualifier(value = DATASOURCE_BEAN) DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public SASOrganization addOrgSASRelationship(String orgIds, String orgKeys) {
        try {
            Organization key = jdbcTemplate.queryForObject(GET_KEY_FOR_ORG_IDS, new DAORowMapper(), id, keys);
            return key;
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public void setJdbcTempate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

}

Question: I am getting java.lang.ClassCastException.

How do I resolve that in this particular code? If you could provide an edited code or a nice explanation.


Solution

  • public Organization addOrg(String orgId, String orgKeys) here I am returning key of type Organization.

    And in the test,

    `mockJdbcTemplate.queryForObject(DAOImpl.GET__KEY_FOR_ORG_IDS, (DAORowMapper) any, expectedId, expectedKeys);`
    

    as you can see the expectedId and expectedKeys, I am passing String but it's expecting Organization not the String.

    The following is the modified test class that worked for me:

    @Test
        public void testAddOrg() {
    
            final Organization expectedOrg = new Organization();
            expectedOrgAttr.setId("7c82facc");
            expectedOrgAttr.setKeys("one");
    
            new Expectations() {{
                mockJdbcTemplate.queryForObject(DAOImpl.GET_KEY_FOR_ORG_IDS, (DAORowMapper) any, expectedOrgAttr.getId(), expectedOrgAttr.getKeys());
                result = expectedOrgAttr;
            }};
    
            Organization actualKey =daoImpl.addOrg(expectedOrgAttr.getId(), expectedOrgAttr.getKeys());
            assertEquals(expectedOrgAttr.getId(), actualKey.getId());
        }
    

    Thank you guys for the quick responses!