java Junit 测试是否进行了方法调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28165522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:07:01  来源:igfitidea点击:

Junit to test if the method call is made

javaunit-testingjunit

提问by JaveDeveloper

I've a transaction class with a validate method. There is a condition in the method that when satisfied will not be calling an alert notification method, which, on the other hand, will only be called when the check condition failed (if condition is false, call the alert and notify the user). I want to test with a JUnit, if that alert method get's called or not. Could someone advise how to proceed, as I'm new to JUnits.

我有一个带有验证方法的事务类。方法中有一个条件,当满足时不会调用警报通知方法,另一方面,只有在检查条件失败时才会调用(如果条件为假,则调用警报并通知用户)。我想使用 JUnit 进行测试,无论是否调用了该警报方法。有人可以建议如何进行,因为我是 JUnits 的新手。

回答by vrudkovsk

Look at mocking libraries. In Mockito it will be like

看看模拟库。在 Mockito 中,它会像

    Notifier mock = Mockito.mock(Notifier.class); 
    ClassUnderTest myClass = new ClassUnderTest(mock);
    myClass.doSomething(-1);
    Mockito.verify(mock).notification(Mockito.eq("Negative value passed!"));
    myClass.doSomething(100);
    Mockito.verifyNoMoreInteractions(mock);

回答by Christian

Use the Mockitoframework. This framework makes your kind of testing much easier.

使用Mockito框架。这个框架使您的测试更加容易。

You can use the verifymethod:

您可以使用以下verify方法:

//Check if called once
verify(dependency, times(1)).yourMethod()

Check the API for more informations.

检查 API 以获取更多信息

回答by Michiel Scheepmaker

From your question it is not clear whether or not the alert notification method belongs to the same class you are testing.

根据您的问题,尚不清楚警报通知方法是否属于您正在测试的同一类。

If the alert notification method is in a different classthan the method you are testing, you can use a mocking library like Mockito to verify the method call, as suggested by other answers.

如果警报通知方法与您正在测试的方法位于不同的类中,您可以使用像 Mockito 这样的模拟库来验证方法调用,如其他答案所建议的那样。

If, on the other hand, the notification method is in the same classas the validation method, I would advice to not verify the call to the notification method, but to verify the outputof this method (you probably want to use a mocking library for this as well, but to verify method calls on any other objects used to create the alert)

另一方面,如果通知方法与验证方法在同一个类中,我建议不要验证对通知方法的调用,而是验证此方法的输出(您可能想要使用模拟库为此,但要验证用于创建警报的任何其他对象的方法调用)

A third possibility is that both methods are in the same class, but you really only want to test that the notification method gets called and not its output, because you feel that notification is a different function that you want to test separately. This would be a sign that your class may be doing too many things, and that it may be best to refactor so that these methods end up in separate classes.

第三种可能性是两个方法都在同一个类中,但您确实只想测试通知方法是否被调用而不是其输出,因为您觉得通知是一个不同的函数,您想单独测试。这将表明您的类可能做了太多事情,最好进行重构以使这些方法最终在单独的类中。