- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 testNG 为我们的测试编写者提供一个“框架”,以加快测试的开发速度。为了实现这一目标,我有一个抽象的 Test 类。所有的书面测试都源自这个 Test 类。这些测试可以通过 testNG 并行运行。现在我注意到,有时在 @BeforeClass 方法中,我的简单“getClass().getName()”方法在并行执行时会返回 null。
private static ThreadLocal<String> className;
@BeforeClass
public void init(){
initName();
className.set(getClass().getName());
log("Starting test " + className.get());
}
private synchronized void initName(){
if(className == null){
className = new ThreadLocal<>();
}
}
当我并行运行一系列测试时,我得到以下输出:
...
Starting test com.mycompany.testsets.LocalTestSet$IntegrationTest3
Starting test null
Starting test com.mycompany.testsets.LocalTestSet$IntegrationTest1
...
一些备注:
Threadlocal ClassName 必须是静态的(我稍后在 @Dataprovider 中使用它的值)
由于它是随机发生的并且可能发生在任何测试中,因此我认为这是一个线程安全问题,但我不知道如何使“getClass().getName()”更加线程安全。
感谢您的帮助
最佳答案
您似乎存在竞争状况。如果一个线程在另一个线程调用 log
之前调用 initName
,则新的 ThreadLocal
将没有值,即。将返回null
。
这里没有线程安全来保护 initName
中的 null
检查。
关于java - getClass().getName() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27706281/
为什么这在 Java 中可行: this.getClass().getClass().getClass().getClass()... 为什么会出现这种无限递归? 只是好奇。 最佳答案 这里没有无限递
我正在测试Object是否等于特定的class类型。例如: @Override public void itemStateChanged(ItemEvent e) { if (e.getSta
详解java中this.getClass()和super.getClass()的实例 前言: 遇到this.getClass()和super.getClass()的返回值感到疑惑,经过探索豁然开
Blockquote public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == nul
我正在尝试比较两个相同的对象。当页面第一次加载时,它们是相等的,但是当我第二次刷新页面时,尽管数据或对象没有改变,但它们并不相等。 我在做什么。 我有一个 CurrentUser obj,我在登录时将
我的 MyClass 类中有 equals() 的 @Override: @Entity( name = "MyClass" ) @Table( name = "my_class" ) public
我在这里真的是指身份平等。 例如,以下是否总是打印 true? System.out.println("foo".getClass() == "fum".getClass()); 最佳答案 是的,类标
getClass().getClassLoader().getResource()和getClass.getResource()有什么区别? 从资源中检索文件时,在什么情况下应该使用哪一个? 最佳答案
这个问题已经有答案了: Different ways of loading a file as an InputStream (6 个回答) 已关闭 5 年前。 我正在浏览 ImageIO,并且在网络
有没有办法创建另一个对象类型的新对象? 示例: Soldier extends Person Accountant extends Person Person 的每个子类都有一个接受(出生日期和死亡日
FXMLLoader类的 load()方法用于加载FXML文件。那么 getClass().getResource() 是在做什么 Parent root = FXMLLoader.load(getC
考虑这段代码: class A { static int i=3; } public class TT extends A { public static void main(Stri
为什么第一行有效而第二行无效: Class c1 = (new Object()).getClass().getClass(); Class> c2 = (new O
我见过类似的问题并且有一个可行的解决方案,但我没有深入理解为什么我的示例中的前四次尝试(c1、c2、c3 和 c4)无法编译。归结为我不理解 this.getClass() 返回的编译时类是什么。 i
这个 Actor 安全吗? private void foo(T value) { final Class aClass = (Class) value.getClass(); } 是否存在
我在学习 Java 时遇到了一件奇怪的事情。考虑以下程序: public class GetClassNameInheritance { public static void main(Str
我编写了以下代码: public class AnonymousClasses { public void sayHello(){ } public interface Greetin
我正在尝试创建其中包含文本字段的对话框。代码如下 private void showBatchDialog() { Dialog dialog = new Dialog(); dial
我正在尝试使用 getClass() 方法并具有以下代码: class parent{} class child extends parent{} public class test { pu
在 Java 中,我想为方法编写测试(简化片段): public class MyClass { private static final Set SOME_SET = new HashSet
我是一名优秀的程序员,十分优秀!