Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.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 JUnit4测试用例_Java_Junit4_Testcase - Fatal编程技术网

Java JUnit4测试用例

Java JUnit4测试用例,java,junit4,testcase,Java,Junit4,Testcase,我需要关于为这段代码创建JUnit4测试用例的帮助 public static int computeValue(int x, int y, int z) { int value = 0; if (x == y) value = x + 1; else if ((x > y) && (z == 0)) value = y + 2; else value = z;

我需要关于为这段代码创建JUnit4测试用例的帮助

public static int computeValue(int x, int y, int z)
{
    int value = 0;

    if (x == y) 
      value = x + 1;
    else if ((x > y) && (z == 0))
           value = y + 2;
         else
           value = z;

     return value;
}
编辑

我想要这样的东西来测试if-else语句

public class TestingTest {

    @Test
        public void testComputeValueTCXX() {

        }

        …

    @Test
        public void testComputeValueTCXX() {

        }

        }

让你开始的东西

首先是一个可能对“新手”更有用的“扩展”版本:

上述测试是ifs级联的第一个案例;其中是众多JUnit断言之一;这是一种hamcrest匹配方法。此外,我将这样编写测试用例:

@Test
public void testXandYEqual() {
  assertThat(ThatClass.computeValue(0, 0, 1), is(1));
}
(主要区别在于:对我来说,单元测试不应该包含任何我不需要的信息;从这个意义上说:我希望它尽可能的纯净、简短、简洁)

基本上,您希望编写不同的测试,以覆盖贯穿逻辑的所有路径。您可以使用许多现有工具之一来确保覆盖所有路径


或者,您也可以查看测试。意思是:不要创建大量的测试方法,其中每个方法只使用不同的参数调用实际方法,而是将所有这些不同的“调用参数”放入表中;然后JUnit从该表中获取所有数据,并在调用测试中的“target”方法时使用这些数据

    @Test
    public void testcomputeValueWithXYandZAsZero() {

        int result = YourClass.computeValue(0, 0, 0);

        Assert.assertEquals(1, result);
    }
确保使用不同的输入集编写测试用例,以便覆盖静态方法的所有分支

您可以使用像EclEmma这样的插件来检查测试的覆盖率。

假设要测试的方法位于类Stackoverflow中。您需要一个名为StackoverflowTest的测试类。这就是它可能的样子:

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author someAuthor
 */
public class StackoverflowTest {

    public StackoverflowTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    // @Test
    // public void hello() {}

    // Here you test your method computeValue()
    // You cover all three cases in the computeValue method

    @Test
    public void computeValueTest() {
        assertTrue(3 == computeValue(2, 2, 0));
        assertTrue(4 == computeValue(3, 2, 0));
        assertTrue(200 == computeValue(1,2,200));
    }
}

在此之前,你有没有写过测试?如果不是,你不应该通过发布问题开始学习,而应该遵循一些基本的教程。请在你的方法中添加一个完整的JavaDoc,并描述这个方法应该做什么和不应该做什么。“单元测试不应该包含我不需要的任何信息;”输入参数的类型和含义以及返回值的类型实际上是unittest应该包含的重要信息。把单元测试看作是一个规范,而不是一个简单的证明。可能合理的解决方案是一些中间立场;例如,将这三个参数作为显式值。但是那些//注释没有任何价值。。。当您以前创建了数百个测试时。“但是那些//注释没有任何价值”当然,它们是为新加入单元测试的OP准备的。另一方面,你应该用至少一条空行将UT的这3个部分视觉上分开。。。
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author someAuthor
 */
public class StackoverflowTest {

    public StackoverflowTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    // @Test
    // public void hello() {}

    // Here you test your method computeValue()
    // You cover all three cases in the computeValue method

    @Test
    public void computeValueTest() {
        assertTrue(3 == computeValue(2, 2, 0));
        assertTrue(4 == computeValue(3, 2, 0));
        assertTrue(200 == computeValue(1,2,200));
    }
}