简体   繁体   English

Mockito存根无效方法

[英]Mockito stubbing void methods

I am currently writing unit tests for my selenium project and I am using Mockito to mock up my webelements and drivers. 我目前正在为我的硒项目编写单元测试,并且正在使用Mockito模拟我的webelements和驱动程序。

The problem I am having is that I have a function that is used to change the radio option in a list of radio buttons but I am having a problem with this. 我遇到的问题是我有一个用于更改单选按钮列表中的单选选项的功能,但是我对此有疑问。 the code looks like 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 a selected and a none selected element to the list*/
    mockElementList.add(mockElement);
    mockElementList.add(selectedMockElement);

    /*The method that is beeing tested*/
    elementSetter.changeRadioState(mockElementList);

    Assert.assertTrue("The radio state was not selected",mockElement.isSelected());
}

What I am trying to do int he doReturn part is to tell the element "mockElement" that when it recieves a click it should allways return true on a isSelected() call. 我要在doReturn部分做的是告诉元素“ mockElement”,当它收到单击时,应该始终在isSelected()调用上返回true。 but since Click() is a void function it won't let me do that. 但由于Click()是一个无效函数,因此我不允许这样做。 Anybody know a way around this? 有人知道解决方法吗?

Ok, it is separate topic - what you are testing and would I mock things so deep. 好的,这是一个单独的主题-您正在测试什么,我将深入探讨一下。

I would just rewrite test like 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);
    // By default mockito will return false but maybe I want to highlight 
    // that this is important
    when(mockElement.isSelected()).thenReturn(false);

    /*Add a selected and a none selected element to the list*/
    mockElementList.add(mockElement);
    mockElementList.add(selectedMockElement);

    /*The method that is beeing tested*/
    elementSetter.changeRadioState(mockElementList);

    verify(selectedMockElement).click();
    // according to test method name I would add 
    // one more verification that something was dis-selected
}

Another variant with state which I 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;
            return null;
        }
    }).when(mockElement).click();
    /*Add a selected and a none selected element to the list*/
    mockElementList.add(mockElement);
    mockElementList.add(selectedMockElement);

    /*The method that is beeing tested*/
    elementSetter.changeRadioState(mockElementList);

    Assert.assertTrue("The radio state was not selected", selected);
    // according to test method name I would add 
    // one more verification that something was dis-selected
}

But again there is misleading in names. 但是名字再次引起误解。 For example I would expect that there are elements which don't become selected when they clicked . 例如,我希望某些元素在单击时不会被选中 Question again about what you are testing 再次询问您要测试的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM