[英]Recursive JUnit Test
So, I'm trying to write 3 test cases to find if the recursive method I wrote is right.因此,我正在尝试编写 3 个测试用例来查找我编写的递归方法是否正确。 I'm not too good with JUnit but this is what I got so far.
我对 JUnit 不太好,但这就是我到目前为止所得到的。
How can I show that my recursive method is right (if it is) in JUnit?如何证明我的递归方法在 JUnit 中是正确的(如果是)?
Here's my Class:这是我的班级:
public class Rhino {
private String co; //The country in which this Rhino was born. Not null
private Rhino parent; //null if this Rhino has no known parent
/** Constructor : an instance born in country c with parent p.
* Precondition : c is not null . */
public Rhino(String c, Rhino p) {
co = c;
parent = p;
}
/** Return the number of Rhinos in this Rhino 's family that
were
* born in country c. This Rhino 's family consists of this
Rhino , its
* parent , its parent 's parent , its parent 's parent 's parent ,
etc . */
public int numCountry(String c) {
if (parent == null){return co.equals(c) ? 1:0;}
return(co.equals(c) ? 1:0) + parent.numCountry(c);
}
}
Here's my JUnit Test so far:到目前为止,这是我的 JUnit 测试:
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class RhinoTest {
@Test
public void testCountry() {
Rhino testCP = new Rhino("USA", null);
assertEquals("USA", testCP.numCountry(""));
assertNotEquals(testCP, null);
}
}
Try this basic test to get you started :试试这个基本测试,让你开始:
public void testCountry() {
Rhino rA = new Rhino("USA", null);
Rhino rB = new Rhino("USA", rA);
Rhino rC = new Rhino("CANADA", rB);
Rhino rD = new Rhino("USA", rC);
int expectedResult = 3;
assertEquals(expectedResult, rD.numCountry("USA"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.