java - Mockito stubbing void methods -
java - Mockito stubbing void methods -
i writing unit tests selenium project , using mockito mock webelements , drivers.
the problem having have function used alter radio alternative in list of radio buttons having problem this. code looks this:
@test public void testchangeradiostate(){ webelement mockelement = mock(webelement.class); list<webelement> mockelementlist = new arraylist<>(); webelement selectedmockelement = mock(webelement.class); /*the when statements*/ when(selectedmockelement.isselected()).thenreturn(true); doreturn(when(mockelement.isselected()).thenreturn(true)).when(mockelement).click(); doreturn(when(selectedmockelement.isselected()).thenreturn(false)).when(mockelement).click(); /*add selected , none selected element list*/ mockelementlist.add(mockelement); mockelementlist.add(selectedmockelement); /*the method beeing tested*/ elementsetter.changeradiostate(mockelementlist); assert.asserttrue("the radio state not selected",mockelement.isselected()); } what trying int doreturn part tell element "mockelement" when recieves click should allways homecoming true on isselected() call. since click() void function won't allow me that. know way around this?
ok, separate topic - testing , mock things deep.
i rewrite test this:
@test public void testchangeradiostate() { webelement mockelement = mock(webelement.class); webelement selectedmockelement = mock(webelement.class); list<webelement> mockelementlist = new arraylist<>(); /*the when statements*/ when(selectedmockelement.isselected()).thenreturn(true); // default mockito homecoming false maybe want highlight // of import when(mockelement.isselected()).thenreturn(false); /*add selected , none selected element list*/ mockelementlist.add(mockelement); mockelementlist.add(selectedmockelement); /*the method beeing tested*/ elementsetter.changeradiostate(mockelementlist); verify(selectedmockelement).click(); // according test method name add together // 1 more verification dis-selected } another variant state think has unnecessary mocks:
boolean selected; @test public void testchangeradiostate() { selected = false; webelement mockelement = mock(webelement.class); webelement selectedmockelement = mock(webelement.class); list<webelement> mockelementlist = new arraylist<>(); /*the when statements*/ when(selectedmockelement.isselected()).thenreturn(true); doanswer(new answer<object>() { public object answer(invocationonmock invocation) { selected = true; homecoming null; } }).when(mockelement).click(); /*add selected , none selected element list*/ mockelementlist.add(mockelement); mockelementlist.add(selectedmockelement); /*the method beeing tested*/ elementsetter.changeradiostate(mockelementlist); assert.asserttrue("the radio state not selected", selected); // according test method name add together // 1 more verification dis-selected } but 1 time again there misleading in names. illustration expect there elements don't become selected when clicked. question 1 time again testing
java selenium mockito
Comments
Post a Comment