以前比没怎么接触过Junit,项目需要所以现学一下
在用Junit时,发现一个调用本地方法的语句
代码如下
import static org.junit.Assert.*;
import org.junit.Test;
public class junitTest {
@Test
public void testCount() {
fail(“...”); //调用“本地”方法
}
}
奇怪我的junitTest类并没有继承Junit的类,但却可以使用fail方法
很快就会发现原来问题出在import语句中,
可以看到第一个import后边跟随着static
自然联想到后边import进来到类
源码:
public class Assert {
...
/**
* Fails a test with the given message.
*
* @param message
* the identifying message for the {@link AssertionError} (<code>null</code>
* okay)
* @see AssertionError
*/
static public void fail(String message) {
throw new AssertionError(message == null ? "" : message);
}
/**
* Fails a test with no message.
*/
static public void fail() {
fail(null);
}
...
}
现在明白原来是这么回事...你懂的...