Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Mockito re存根方法已与thenthrow存根_Java_Mocking_Mockito_Stubbing - Fatal编程技术网

Java Mockito re存根方法已与thenthrow存根

Java Mockito re存根方法已与thenthrow存根,java,mocking,mockito,stubbing,Java,Mocking,Mockito,Stubbing,我遇到了莫基托的问题。 我正在开发一个web应用程序。在我的测试中,用户管理是模拟的。 在某些情况下,我必须更改getLoggedInUser()方法返回的用户 问题是,我的getLoggedInUser()方法也会抛出AuthenticationException 因此,当我尝试从无用户切换到某个用户时 when(userProvider.getLoggedInUser()).thenReturn(user); 抛出异常,因为userProvider.getLoggedInUser()已被t

我遇到了莫基托的问题。 我正在开发一个web应用程序。在我的测试中,用户管理是模拟的。 在某些情况下,我必须更改
getLoggedInUser()
方法返回的用户

问题是,我的
getLoggedInUser()
方法也会抛出
AuthenticationException

因此,当我尝试从无用户切换到某个用户时

when(userProvider.getLoggedInUser()).thenReturn(user);
抛出异常,因为
userProvider.getLoggedInUser()
已被
thenTrow()存根。

有没有办法告诉
方法何时不关心异常


提前感谢-István

我对你的问题的第一反应是,听起来你好像在一次测试中做了太多

为了便于测试和简化,每个测试应该只测试一件事情。这是相同的。我经常发现程序员试图在一个测试中测试多个东西,并因此遇到各种各样的问题。因此,每个单元测试方法都应遵循以下流程:

  • 为测试设置单个场景
  • 调用正在测试的类以触发正在测试的代码
  • 验证行为
  • 因此,在您的情况下,我希望看到至少两个测试。其中一个
    getLoggedInUser()
    返回用户,另一个
    getLoggedInUser()
    抛出异常。这样,在模拟中尝试模拟不同的行为就不会有问题


    第二个想法是,头脑中的春天不是存根。考虑改用expect,因为您可以设置一系列的expect。也就是说,第一个调用返回一个用户,第二个调用抛出一个异常,第三个调用返回一个不同的用户,等等。

    在新的Mockito版本中,您可以使用存根连续调用在第一个can上抛出异常,并在第二个调用上返回值

    when(mock.someMethod("some arg"))
        .thenThrow(new RuntimeException())
        .thenReturn("foo");
    

    有没有办法告诉方法什么时候不关心异常

    要真正回答这个问题:

    导入静态org.hamcrest.Matchers.equalTo;
    导入静态org.hamcrest.Matchers.is;
    导入静态org.junit.Assert.assertThat;
    导入静态org.mockito.ArgumentMatchers.any;
    导入静态org.powermock.api.mockito.PowerMockito.mock;
    导入静态org.powermock.api.mockito.PowerMockito.when;
    导入org.junit.Test;
    导入org.mockito.mockito;
    导入java.util.ArrayList;
    公共类MyTest{
    @试验
    公共无效遗嘱(){
    //设置
    ArrayList=mock(ObjectArrayList.class);
    when(list.indexOf(any())。然后返回(6);
    when(list.indexOf(any())。然后返回(12);
    //执行
    int index=list.indexOf(新对象());
    //核实
    资产(指数,等于(12));
    }
    @试验
    公共无效测试b(){
    //设置
    ArrayList=mock(ObjectArrayList.class);
    when(list.add(any()).thenthow(newassertionError(“无法摆脱我”));
    when(list.add(any())。然后返回(true);
    //执行
    添加(新对象());
    }
    @试验
    公共void testC(){
    //设置
    ArrayList=mock(ObjectArrayList.class);
    when(list.add(any()).thenthow(newassertionError(“无法摆脱我”));
    Mockito.reset(列表);
    when(list.add(any())。然后返回(true);
    //执行
    添加(新对象());
    }
    /**
    *存在是为了解决模拟ArrayList
    *需要强制转换,这会导致“未选中”的警告,该警告只能被抑制。。。
    */
    类ObjectArrayList扩展了ArrayList{
    }
    }
    
    TestB
    由于无法摆脱的断言而失败
    TestC
    显示如何使用
    reset
    方法重置模拟并删除其上的
    thenthrown
    命令


    请注意,在我举的一些更复杂的例子中,重置似乎并不总是有效。我怀疑这可能是因为他们使用的是
    PowerMockito.mock
    而不是
    Mockito.mock

    谢谢你们的回答!总而言之:很可能是因为软件设计不好,我需要重新发布该方法。但现在对我来说很容易,而且测试看起来也很干净。我做了更多的研究,发现了Mockito.reset(T…mocks)方法,这对我来说很有用。下次我会想出一些更简单的设计:)请举一个使用“expect”或文档/教程链接的例子:)