[英]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? 是否可以从junit测试中调用静态方法而无需指定其类?
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 对于类型LightsOutModelTest,方法checkWin()未定义
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?
是否没有办法从类中导入所有方法,因为我尝试执行的方法似乎无效?
You need to use a static import of the method: 您需要使用该方法的静态导入:
import static lightsOut.LightsOutModel.checkWin;
Once you import, you can directly use them, 导入后,您可以直接使用它们,
checkWin();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.