spencer.sm
spencer.sm

Reputation: 20526

JUnit testing method is undefined

I keep getting an error when I try to test a method I created. I have a more complicated method, but I get the same error even with a simple method I created so that's what I showed below.

Method

package example;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Scanner;

public class GraphingMethods
{

    public static int multiply (int x, int y)
    {
         return x * y;
    }
}

Test

package example;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Scanner;
import org.junit.Test;
import example.GraphingMethods;

public class Tests
{
    @Test
    public void testMultiply()
    {
        assertEquals(10, multiply(2, 5));
    }
}


When I hover the mouse over multiply(2, 5) it shows the following message in Eclipse.
What am I missing?

Upvotes: 2

Views: 4489

Answers (2)

fgomboe
fgomboe

Reputation: 1

Since multiply is a static method of class GraphingMethods, as Timothy Stepanski suggests, you need to prepend the class name (it is not necessary to prepend also the package name, as they both belong to the same package):

GraphingMethods.multiply(2, 5)

Or you can opt to import the method statically to use it without prepending the class name, as follows:

import static example.GraphingMethods.multiply;

Or, if you want to use multiple methods from the same class:

import static example.GraphingMethods.*;

Upvotes: 0

Timothy Stepanski
Timothy Stepanski

Reputation: 1196

Are they in the same class? If not, you'll need to address the multiply by 3 part name

{package}.{class}.multiply

Upvotes: 3

Related Questions