如何为这段代码编写适当的 JUnit 测试?
How Can I Write a Proper JUnit Test for this code?
我是编程新手。我必须为此程序编写 JUnit 测试以找到 GCD,如下所示:
public class CoprimeNumbersTest {
/**
* Given two integers, this returns true if they are relatively prime and false if they are not. Based upon the first
* webpage I found ({@link "https://primes.utm.edu/notes/faq/negative_primes.html"}), the primality of negative
* numbers is up for debate. This method will not treat negatives differently.
*
* @param a First integer to be tested
* @param b Second integer to be tested
* @return True when the greatest common divisor of these numbers is 1; false otherwise.
*/
public boolean isCoprime(int a, int b) {
// Continue using Euclid's algorithm until we find a common divisor
while (b != 0) {
// Remember b's value
int temp = b;
// Set b to the remainder of dividing a by b (e.g., a mod b).
b = a % b;
// Set a equal to b's old value.
a = temp;
}
// The gcd is the value in a. If this is 1 the numbers are coprime.
if (a == 1) {
return true;
}
// When they are not 1, they have a common divisor.
else {
return false;
}
}
}
这是我能想到的:
public class CoPrimetest {
@Test
public void testing() {
assetEquals(1, GCDFinder.CoprimeNumbersTest);
}
}
是否有任何我遗漏的方法可以帮助改进我的代码?
您需要实际调用您的方法,就像在普通代码中一样。 (以下代码没有测试,不知道1和1其实是不是co-prime。)
public class CoPrimetest {
@Test
public void testing() {
CoprimeNumbersTest instance = new CoprimeNumbersTest();
boolean result = instance.isCoprime( 1, 1 );
boolean expected = true;
assertEquals( expected, result );
}
}
针对 CoprimeNumbersTest
class 中的 isCoprime
方法编写的示例测试方法可能是
@org.junit.Test
public void isCoprime() throws Exception {
org.junit.Assert.assertEquals(true, new CoprimeNumbersTest().isCoprime(3,4));
}
由于 return 类型的方法是 boolean
,您可以断言它等于 true
或 false
。
建议,尝试使用这些输入 (3,4)
干燥 运行 isCoprime
方法,并找出所有语句都已涵盖的内容。基于此推断如果您提供的输入将涵盖其余的陈述。这应该有助于用单元测试覆盖代码。
附带说明一下,尝试重命名您的 classes 以实践更好的命名约定,例如 GreatestCommonDivisor.java
和 GreatestCommonDivisorTest.java
也将它们链接起来。
我是编程新手。我必须为此程序编写 JUnit 测试以找到 GCD,如下所示:
public class CoprimeNumbersTest {
/**
* Given two integers, this returns true if they are relatively prime and false if they are not. Based upon the first
* webpage I found ({@link "https://primes.utm.edu/notes/faq/negative_primes.html"}), the primality of negative
* numbers is up for debate. This method will not treat negatives differently.
*
* @param a First integer to be tested
* @param b Second integer to be tested
* @return True when the greatest common divisor of these numbers is 1; false otherwise.
*/
public boolean isCoprime(int a, int b) {
// Continue using Euclid's algorithm until we find a common divisor
while (b != 0) {
// Remember b's value
int temp = b;
// Set b to the remainder of dividing a by b (e.g., a mod b).
b = a % b;
// Set a equal to b's old value.
a = temp;
}
// The gcd is the value in a. If this is 1 the numbers are coprime.
if (a == 1) {
return true;
}
// When they are not 1, they have a common divisor.
else {
return false;
}
}
}
这是我能想到的:
public class CoPrimetest {
@Test
public void testing() {
assetEquals(1, GCDFinder.CoprimeNumbersTest);
}
}
是否有任何我遗漏的方法可以帮助改进我的代码?
您需要实际调用您的方法,就像在普通代码中一样。 (以下代码没有测试,不知道1和1其实是不是co-prime。)
public class CoPrimetest {
@Test
public void testing() {
CoprimeNumbersTest instance = new CoprimeNumbersTest();
boolean result = instance.isCoprime( 1, 1 );
boolean expected = true;
assertEquals( expected, result );
}
}
针对 CoprimeNumbersTest
class 中的 isCoprime
方法编写的示例测试方法可能是
@org.junit.Test
public void isCoprime() throws Exception {
org.junit.Assert.assertEquals(true, new CoprimeNumbersTest().isCoprime(3,4));
}
由于 return 类型的方法是 boolean
,您可以断言它等于 true
或 false
。
建议,尝试使用这些输入 (3,4)
干燥 运行 isCoprime
方法,并找出所有语句都已涵盖的内容。基于此推断如果您提供的输入将涵盖其余的陈述。这应该有助于用单元测试覆盖代码。
附带说明一下,尝试重命名您的 classes 以实践更好的命名约定,例如 GreatestCommonDivisor.java
和 GreatestCommonDivisorTest.java
也将它们链接起来。