spencer.sm
spencer.sm

Reputation: 20526

Static method call from JUnit test without specifying class

Is it possible to call a static method from a junit test without specifying it's class?

The following code works:

package lightsOut;

import static org.junit.Assert.*;
import org.junit.Test;
import lightsOut.LightsOutModel;

public class LightsOutModelTest {

    @Test
    public void testLightsOutModel1(){
        assertTrue(LightsOutModel.checkWin()); // Note here
    }

}

But when I remove the class from the following line it shows an error.

        assertTrue(checkWin()); // Note here

The error is:
The method checkWin() is undefined for the type LightsOutModelTest

Do I always have to specify the class on the static method call? Isn't there a way to import all the methods from the class because the way I tried to do it doesn't seem to work?

Upvotes: 0

Views: 1084

Answers (1)

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

You need to use a static import of the method:

import static lightsOut.LightsOutModel.checkWin;

Once you import, you can directly use them,

checkWin();

Here is the official reference

Upvotes: 2

Related Questions