Mockito ClassCastException with casting field -
i'm trying write unit test following code
public desiredcapabilities findcapability(platforms platform, types type, string browser, double platformversion) throws exception{ criteria criteria = getsession().createcriteria(browsercapabilities.class) .add(restrictions.eq("status", icommonconstants.db_status_active)) .add(restrictions.eq("platformname", platform.getvalue())) .add(restrictions.eq("browsername", browser)) .add(restrictions.eq("platformversion", platformversion)) .addorder(order.desc("browserversion")) .setmaxresults(1); object res = criteria.uniqueresult(); if(res!=null) return this.preparebrowsercapability((browsercapabilities)res); return null; }
the mock code is
private browsercapabilitydao mockbrowsercapability; private session mocksession; private criteria mockcriteria; private criterion mockcriterion; @before public void init() { mockbrowsercapability = mockito.spy(new browsercapabilitydao()); mocksession = mockito.mock(session.class, mockito.returns_deep_stubs); mockcriteria = mockito.mock(criteria.class, mockito.returns_deep_stubs); mockcriterion = mockito.mock(criterion.class, mockito.returns_deep_stubs); } @test public void testfindcapability() throws exception { desiredcapabilities desiredcapability = new desiredcapabilities(); browsercapabilities mockbrowsercapabilities = getmockbrowsercapabilities(); //stub mockito.doreturn(mocksession).when(mockbrowsercapability).getsession(); mockito.when(mocksession.createcriteria(mockito.eq(browsercapabilities.class))).thenreturn(mockcriteria); mockito.doreturn(mockcriteria).when(mockcriteria).add(mockcriterion); mockito.when(mockcriteria.addorder(mockito.any(order.class))).thenreturn(mockcriteria); mockito.when(mockcriteria.setmaxresults(mockito.anyint())).thenreturn(mockcriteria); mockito.when(mockcriteria.uniqueresult()).thenreturn((object)mockbrowsercapabilities); //mockito.doreturn(desiredcapability).when(mockbrowsercapability).preparebrowsercapability(mockbrowsercapabilities); mockbrowsercapability.findcapability(platforms.windows, types.computer, "ff", 10.0); } private browsercapabilities getmockbrowsercapabilities() { browsercapabilities mockbrowsercapabilities = new browsercapabilities(); mockbrowsercapabilities.setbrowsername("browsername"); mockbrowsercapabilities.setbrowserversion("browserversion"); return mockbrowsercapabilities; }
but i'm getting classcastexception @ line
return this.preparebrowsercapability((browsercapabilities)res);
cannot cast com.common.xxx.persistence.beans.browsercapabilities
Comments
Post a Comment