Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.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进行测试时监视方法参数?_Java_Unit Testing_Junit_Mockito - Fatal编程技术网

Java 在使用Mockito进行测试时监视方法参数?

Java 在使用Mockito进行测试时监视方法参数?,java,unit-testing,junit,mockito,Java,Unit Testing,Junit,Mockito,假设我有一个像这样的班级 public class FooBar { public int getMethod(List<String> code){ if(code.size() > 100) throw new Exception; return 0; } } 公共类FooBar{ 公共int getMethod(列表代码){ 如果(code.size()>100) 抛出新异常; 返回0

假设我有一个像这样的班级

public class FooBar {

    public int getMethod(List<String> code){

        if(code.size() > 100)
            throw new Exception;

            return 0;
    }
}
公共类FooBar{
公共int getMethod(列表代码){
如果(code.size()>100)
抛出新异常;
返回0;
}
}
我有一个这样的测试班

@RunWith(PowerMockRunner.class)
@PrepareForTest(FooBar.class)
public class FooBarTest{

    FooBar fooBarInstance;

    @Before
    public void setUp() {

        //MockitoAnnotations.initMocks(this);
        fooBarInstance = new FooBar();   
    }

    @Test(expected = Exception.class)
    public void testGetCorrelationListCodesParameter() {

        List<String> codes = Mockito.spy(new ArrayList<String>());
        Mockito.doReturn(150).when(codes).size();
        fooBarInstance.getMethod(codes);
    }
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(FooBar.class)
公共级食品交换师{
FooBar fooBarInstance;
@以前
公共作废设置(){
//initMocks(this);
fooBarInstance=新的FooBar();
}
@测试(预期=异常.class)
public void testGetCorrelationListCodesParameter(){
列表代码=Mockito.spy(新的ArrayList());
doReturn(150).when(codes).size();
fooBarInstance.getMethod(代码);
}
}

如何使此测试方法引发异常?我花了好几个小时来做这件事。不管怎样,还是谢谢你。

不需要监视,嘲笑就足够了。正如@David所说,对于值对象,不需要也不建议进行模拟

使用
@Test(expected=Exception.class)
有许多缺点,当异常从非预期位置抛出时,测试可以通过。测试不工作,但显示为绿色

我更喜欢BDD风格的测试

(…)与使用try/catch块相比

  • 测试更简洁,更容易阅读
  • 测试不能被丢失的断言损坏。假设您忘记在预期会引发异常的方法调用后面键入fail()
(…)与捕获和验证异常的特定于测试运行程序的机制相比

  • 单个测试可以验证多个抛出的异常
  • 测试可以在捕获异常后验证抛出异常的属性
  • 测试可以指定必须通过哪个方法调用抛出异常
  • 测试不依赖于特定的测试运行程序(JUnit4、TestNG)

间谍活动是不需要的,嘲笑就足够了。正如@David所说,对于值对象,不需要也不建议进行模拟

使用
@Test(expected=Exception.class)
有许多缺点,当异常从非预期位置抛出时,测试可以通过。测试不工作,但显示为绿色

我更喜欢BDD风格的测试

(…)与使用try/catch块相比

  • 测试更简洁,更容易阅读
  • 测试不能被丢失的断言损坏。假设您忘记在预期会引发异常的方法调用后面键入fail()
(…)与捕获和验证异常的特定于测试运行程序的机制相比

  • 单个测试可以验证多个抛出的异常
  • 测试可以在捕获异常后验证抛出异常的属性
  • 测试可以指定必须通过哪个方法调用抛出异常
  • 测试不依赖于特定的测试运行程序(JUnit4、TestNG)

列表是一个值对象。我们不应该嘲笑它。如果您准备构建一个大小超过100的列表,那么您可以编写整个测试而无需模拟任何内容

另外,我更喜欢使用JUnit的
ExpectedException
机制,因为它允许您检查测试方法的哪一行抛出了异常。这比将参数传递给
@Test
注释要好,后者只允许您检查异常是否在方法中的某个位置抛出

public class FooBarTest {
    @Rule 
    public ExpectedException exceptionRule = ExpectedException.none();
    private FooBar toTest = new FooBar();

    @Test
    public void getMethodThrowsException_whenListHasTooManyElements() {
        List<String> listWith101Elements = 
            new ArrayList<String>(Arrays.asList(new String[101]));

        exceptionRule.expect(Exception.class);
        toTest.getMethod(listWith101Elements);
    }
}
公共类食品测试{
@统治
public ExpectedException exceptionRule=ExpectedException.none();
private FooBar toTest=new FooBar();
@试验
public void getMethodThrowsException\u(当列表为ToomanyElements()时){
List ListWith101元素=
新的ArrayList(Arrays.asList(新字符串[101]);
exceptionRule.expect(Exception.class);
toTest.getMethod(包含101个元素的列表);
}
}

列表是一个值对象。我们不应该嘲笑它。如果您准备构建一个大小超过100的列表,那么您可以编写整个测试而无需模拟任何内容

另外,我更喜欢使用JUnit的
ExpectedException
机制,因为它允许您检查测试方法的哪一行抛出了异常。这比将参数传递给
@Test
注释要好,后者只允许您检查异常是否在方法中的某个位置抛出

public class FooBarTest {
    @Rule 
    public ExpectedException exceptionRule = ExpectedException.none();
    private FooBar toTest = new FooBar();

    @Test
    public void getMethodThrowsException_whenListHasTooManyElements() {
        List<String> listWith101Elements = 
            new ArrayList<String>(Arrays.asList(new String[101]));

        exceptionRule.expect(Exception.class);
        toTest.getMethod(listWith101Elements);
    }
}
公共类食品测试{
@统治
public ExpectedException exceptionRule=ExpectedException.none();
private FooBar toTest=new FooBar();
@试验
public void getMethodThrowsException\u(当列表为ToomanyElements()时){
List ListWith101元素=
新的ArrayList(Arrays.asList(新字符串[101]);
exceptionRule.expect(Exception.class);
toTest.getMethod(包含101个元素的列表);
}
}

什么调用应该引发异常?codes.size()应该返回100以上?您在没有Powermock的情况下尝试过吗?或
当(code.size())时。然后返回(150)
。你为什么不使用一个简单的模拟列表而不是间谍呢?问题是我还有其他测试要完成,我通常使用PowerMockRunner,我不得不说我是测试领域的新手:)。因此,试图弄清楚mockito的概念究竟什么调用应该引发异常?codes.size()应该返回100多个?您在没有Powermock的情况下尝试过吗?或
当(code.size())时。然后返回(150)
。你为什么不使用一个简单的模拟列表而不是间谍呢?问题是我还有其他测试要完成,我通常使用PowerMockRunner,我不得不说我是测试领域的新手:)。因此,试图弄清Mockitohi MariuszS的概念,使用
PowerMockRunner
public class FooBarTest {
    @Rule 
    public ExpectedException exceptionRule = ExpectedException.none();
    private FooBar toTest = new FooBar();

    @Test
    public void getMethodThrowsException_whenListHasTooManyElements() {
        List<String> listWith101Elements = 
            new ArrayList<String>(Arrays.asList(new String[101]));

        exceptionRule.expect(Exception.class);
        toTest.getMethod(listWith101Elements);
    }
}