Kommander Kitten
Kommander Kitten

Reputation: 283

JUnit Test not able to find class to test

I am currently writing a JUnit Unit Test Case for a simple method. Here is the method in question:

public static Integer[] findUnpairedNumbers(Integer[] numbers) {
    // catches if passed array is empty or uninitialized 
    if (numbers == null) {
        throw new IllegalArgumentException();
    }
    // HashSet initialized to store unpaired numbers
    HashSet<Integer> result = new HashSet<Integer>();
    // Loop through numbers array
    for (int next: numbers) {
        // if HashSet already contains next element, remove that element from the HashSet
        if (result.contains(next)) {
            result.remove(next);
        }
        // Otherwise, add it to the HashSet
        else {
            result.add(next);
        }
    }
    // return the HashSet of unpaired numbers, converting to back to Integer array
    return result.toArray(new Integer[result.size()]);
}

Here is the JUnit test I have written:

package program;

import static org.junit.Assert.*;

import org.junit.Test;

public class FindUnpairedTest {

    @Test
    public void test() {
        Integer[] numbers = {1, 2, 4, 5, 7, 7, 8, 8, 9, 1, 5};
        Integer[] unpairedCheck = {2, 4};
        Integer[] unpaired = findUnpairedNumbers(numbers);

        if (unpaired != unpairedCheck) {
            fail();
        }
    }
}

However, the findUnpairedNumbers class is not recognized by the test class. It is in the same package. Do I need to import anything else? Where am I going wrong here?

Also please let me know if any syntax is unorthodox or if there are other ways I can improve my test case.

Upvotes: 1

Views: 2528

Answers (2)

acdcjunior
acdcjunior

Reputation: 135742

However, the findUnpairedNumbers class is not recognized by the test class. It is in the same package. Do I need to import anything else?

findUnpairedNumbers is a static method of some class. In order to call it, you must specify in what class it exists.

Like this:

Integer[] unpaired = NameOfTheClass.findUnpairedNumbers(numbers);

The test method

Also please let me know if any syntax is unorthodox or if there are other ways I can improve my test case.

Don't use fail() directly. Usually, we call a assertSomething() method from JUnit (e.g. assertEquals()) at the assertion phase of our test. In your case, I suggest assertArrayEquals():

@Test
public void test() {
    Integer[] numbers = {1, 2, 4, 5, 7, 7, 8, 8, 9, 1, 5};
    Integer[] unpaired = findUnpairedNumbers(numbers);

    Integer[] expectedUnpaired = {2, 4};
    assertArrayEquals(expectedUnpaired, unpaired);
}

Notice how the test becomes cleaner. I also suggest you rename your test method name to something more descriptive of the behavior:

@Test
public void findUnpairedNumbersLeavesOnlyNumbersWithoutPairs() { ...

This would make it easier for maintainers to get the intent of the test and the expected behavior (in case it fails in the future).


More on the static problem

A more complete explanation/example:

Say the findUnpairedNumbers method belongs to a class called FindUnpaired:

package program;
public class FindUnpaired {
    public static Integer[] findUnpairedNumbers(Integer[] numbers) {
        ...
        return result.toArray(new Integer[result.size()]);
    }
}

Now, in your test class you can use it:

package program;
import static org.junit.Assert.*;
import org.junit.Test;
public class FindUnpairedTest {
    @Test
    public void test() {
        Integer[] numbers = {1, 2, 4, 5, 7, 7, 8, 8, 9, 1, 5};
        Integer[] unpairedCheck = {2, 4};
        Integer[] unpaired = FindUnpaired.findUnpairedNumbers(numbers);
//                           ^^^^^^^^^^^^----------------------------------- changed here
        if (unpaired != unpairedCheck) {
            fail();
        }
    }
}

Other considerations:

  • You can use FindUnpaired directly because we are assuming the test class is at the same package. If the packages were different, you'd have to import FindUnpaired at the test class.

    package someOtherPackage;
    import program.FindUnpaired;
    public class FindUnpairedTest {
    
  • Another option is to use a static import of the findUnpairedNumbers method. If you follow this route, you don't need to prefix when calling it (call it like it was a static method declared in the current class).

    import static program.FindUnpaired.findUnpairedNumbers; // static import
    public class FindUnpairedTest {
        ...
        Integer[] unpaired = findUnpairedNumbers(numbers); // no need to prefix
    

Upvotes: 3

Milind Gokhale
Milind Gokhale

Reputation: 585

  1. you should create an object to call the method you want to test in the testClass then call the method obj.findUnpairedNumbers()

or

  1. you should make the method being called as static and call YourClass.findUnpairedNumbers()

Upvotes: 0

Related Questions