This seems like it shouldn't compile and run as Object
does not have a fail()
method. At compile time is something funky happening? (I am using NetBeans):
这似乎不应该编译和运行,因为Object没有fail()方法。在编译时是一个时髦的事情发生? (我使用的是NetBeans):
import static org.junit.Assert.*;
import org.junit.Test;
public class Test {
@Test
public void hello() {
fail();
}
}
Regards,
问候,
Guido
圭多
3 个解决方案
#1
18
Your import static
line imports all static members of the Assert
class into the static namespace of your compilation unit. The fail()
call refers to Assert.fail()
.
导入静态行将Assert类的所有静态成员导入到编译单元的静态名称空间中。 fail()调用引用Assert.fail()。
The confusion you are experiencing regarding where fail()
is defined is precisely why I don't usually recommend using import static
. In my own code, I usually import the class and use it to invoke the static methods:
您遇到的关于fail()定义位置的困惑正是我通常不建议使用import static的原因。在我自己的代码中,我通常导入该类并使用它来调用静态方法:
import org.junit.Assert;
import org.junit.Test;
public class Test {
@Test
public void hello() {
Assert.fail();
}
}
Much more readable.
更具可读性。
However, as JB Nizet points out, it is fairly common practice to use import static
for JUnit's assertions; when you write and read enough JUnit tests, knowing where the assertion methods come from will become second nature.
但是,正如JB Nizet指出的那样,对JUnit的断言使用import static是很常见的做法。当你编写和阅读足够的JUnit测试时,知道断言方法的来源将成为第二天性。
#2
6
This is perfectly correct and it will run and compile - I already checked using eclipse. The reason is the static import:
这是完全正确的,它将运行和编译 - 我已经使用eclipse进行了检查。原因是静态导入:
import static org.junit.Assert.*;
that adds all the static fields or methods from the org.junit.Assert class - hence including fail() method.
它添加了org.junit.Assert类中的所有静态字段或方法 - 因此包括fail()方法。
Nevertheless a problem that might occur is the fact that the name of your test class is the same as the name of the annotation
然而,可能出现的问题是测试类的名称与注释的名称相同
@Test
hence it will generate an error:
因此它会产生一个错误:
The import org.junit.Test conflicts with a type defined in the same file
导入org.junit.Test与同一文件中定义的类型冲突
#3
0
This error is coming because your classname and annotation name are same(Test).Change your class name to 'Test1' or other than Test.
此错误即将发生,因为您的类名和注释名称相同(测试)。将您的类名更改为“Test1”或Test以外的名称。
#1
18
Your import static
line imports all static members of the Assert
class into the static namespace of your compilation unit. The fail()
call refers to Assert.fail()
.
导入静态行将Assert类的所有静态成员导入到编译单元的静态名称空间中。 fail()调用引用Assert.fail()。
The confusion you are experiencing regarding where fail()
is defined is precisely why I don't usually recommend using import static
. In my own code, I usually import the class and use it to invoke the static methods:
您遇到的关于fail()定义位置的困惑正是我通常不建议使用import static的原因。在我自己的代码中,我通常导入该类并使用它来调用静态方法:
import org.junit.Assert;
import org.junit.Test;
public class Test {
@Test
public void hello() {
Assert.fail();
}
}
Much more readable.
更具可读性。
However, as JB Nizet points out, it is fairly common practice to use import static
for JUnit's assertions; when you write and read enough JUnit tests, knowing where the assertion methods come from will become second nature.
但是,正如JB Nizet指出的那样,对JUnit的断言使用import static是很常见的做法。当你编写和阅读足够的JUnit测试时,知道断言方法的来源将成为第二天性。
#2
6
This is perfectly correct and it will run and compile - I already checked using eclipse. The reason is the static import:
这是完全正确的,它将运行和编译 - 我已经使用eclipse进行了检查。原因是静态导入:
import static org.junit.Assert.*;
that adds all the static fields or methods from the org.junit.Assert class - hence including fail() method.
它添加了org.junit.Assert类中的所有静态字段或方法 - 因此包括fail()方法。
Nevertheless a problem that might occur is the fact that the name of your test class is the same as the name of the annotation
然而,可能出现的问题是测试类的名称与注释的名称相同
@Test
hence it will generate an error:
因此它会产生一个错误:
The import org.junit.Test conflicts with a type defined in the same file
导入org.junit.Test与同一文件中定义的类型冲突
#3
0
This error is coming because your classname and annotation name are same(Test).Change your class name to 'Test1' or other than Test.
此错误即将发生,因为您的类名和注释名称相同(测试)。将您的类名更改为“Test1”或Test以外的名称。