Dan - 4 years ago 132
Java Question

How Can I Write a Proper JUnit Test for finding the GCD

I'm new to programming. For homework, I have to write a JUnit test for this program to find the GCD, shown here :

``````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;
}
}
}
``````

This is what I could come up with:

``````public class CoPrimetest {

@Test
public void testing() {
}

}
``````

I know this isn't the right code, but I honestly don't know how to approach this problem.

You need to actually call your method, just like in normal code. (The following code is not tested, I don't know if 1 and 1 are actually co-prime.)

``````public class CoPrimetest {

@Test
public void testing() {