java 你如何用junit测试if语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27605211/
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 12:06:25  来源:igfitidea点击:

how do you test if statements with junit?

javajunit

提问by BlueShark

I can't seem to find any tutorials online that address this

我似乎无法在网上找到任何解决此问题的教程

I have this ifstatement:

我有这样的if声明:

if (basket.getCustomerId() != null) {
                Basket exBasket = findBasketByCustomerId(basket.getCustomerId());
                if (exBasket != null && exBasket.getBasketId() != null) {
                    return exBasket;
                }

and I want to write a unit test that will test each individual line to see if it is doing the right thing.

我想编写一个单元测试来测试每一行,看看它是否在做正确的事情。

Any ideas?

有任何想法吗?

@Test
    public void testBasketWithANullCustomerId(){
        basketInformationServiceImpl.createBasket(mockBasket);
        assertNotNull(mockBasket.getCustomerId());
    }

回答by chiastic-security

The purpose of unit testing isn't to test statementsbut methods. The right thing to do is not to consider this line, but to consider the method that it appears in, and ask what you want that method to do: what sort of input does it take, and what sort of output should it produce?

单元测试的目的不是测试语句,而是测试方法。正确的做法不是考虑这一行,而是考虑它出现的方法,并询问您希望该方法做什么:它需要什么样的输入,它应该产生什么样的输出?

Then write tests that take some typicalinputs, and check that they give correct outputs, and also some edge caseinputs (like null, 0, Integer.MAX_VALUE, etc.) and check that you get the right outputs for those too.

然后编写采用一些典型输入的测试,并检查它们是否提供正确的输出,以及一些边缘情况输入(如null、 0 Integer.MAX_VALUE、 等),并检查您是否也获得了正确的输出。

If this is your entire method (which actually it can't be, but if it's the essence of it), I'd test:

如果这是你的整个方法(实际上它不可能,但如果它是它的本质),我会测试:

  • a basketwith a nullcustomer ID;
  • a nullbasket (unless you're certain this can never happen), because currently this code will give a NullPointerException;
  • a basket that has a customer ID that should allow you to find a known exBasket;
  • a basket that has a customer ID that will return an exBasketof null;
  • a basket that has a customer ID that will return an exBasketthat isn't null, but whose basket ID is null.
  • abasket带有null客户 ID;
  • 一个null篮子(除非您确定这永远不会发生),因为目前此代码将给出NullPointerException;
  • 一个带有客户 ID 的篮子,可以让您找到已知的exBasket
  • 具有客户ID,将返回的一篮子exBasketnull;
  • 一个具有客户 ID 的购物篮,该 ID 将返回一个exBasket不为空但其购物篮 ID 为 的null

回答by rchang

Assuming this is an excerpt from a method you're trying to test, it sounds like you need at least two test cases - one where basket.getCustomerId()is expected to be null and one where it isn't. In both cases you should be able to test the method's return value (I'm assuming a Basketwill be returned as well in the event getCustomerId()is null) against the expected outcome.

假设这是您尝试测试的方法的摘录,听起来您至少需要两个测试用例 - 一个basket.getCustomerId()预计为空,一个不是。在这两种情况下,您都应该能够针对预期结果测试该方法的返回值(我假设 aBasket也将在事件getCustomerId()为 null 时返回)。

回答by Brandon McKenzie

To test in the way you seem to want to (emphasizing line coverage), you need a test for each situation you want to test (for example, one test for a null basketId, another for a not null basketId, another for another case you wish to test).

要以您似乎想要的方式进行测试(强调线路覆盖率),您需要针对要测试的每种情况进行测试(例如,一个测试用于空的篮子 ID,另一个用于非空的篮子 ID,另一个用于您的另一种情况希望测试)。

Mocking frameworks (such as Mockito) can be used to to set preconditions for a test. For an example of one case you can say

模拟框架(例如Mockito)可用于为测试设置先决条件。对于一个案例的例子,你可以说

@Test
public void myTestWithNonNullBasketId() {
    Basket inputBasket = Mockito.mock(Basket.class);
    when(inputBasket.getBasketId()).thenReturn(1); //Or whatever we want basketId to return, null if you want to check that case.
    ... //More mocking as needed, presumably to dictate whatever findBasketByCustomerId might return.

    //Call the method you are testing, use org.junit.Assert assertions to check outputs.
}