【问题标题】:Using @Override in JUnit4在 JUnit4 中使用 @Override
【发布时间】:2011-04-01 21:14:49
【问题描述】:

我正在使用 Selenium RC 和 JUnit 编写测试。如果@Test 失败,我想截屏,但我想使用从Test 类传递的特定截屏名称。

目前我有:

@Rule  
public ScreenshotRule email = new ScreenshotRule();  

@Test  
public void testLogin() throws InterruptedException {  
    MyTest someTest = new MyTest(selenium);   
    someTest.someMethod (); // if anything assert fails in this method, i want to take a shot and call it a name which i will pull from the MyTest class.
}

这是Screenshot 类:

public class ScreenshotRule extends TestWatchman {  
    @Override  
    public void failed(Throwable e, FrameworkMethod method) {  
        System.out.println(method.getName() + " failed");  
        // At this point I wish to take a screenshot and call  
    }  
}

那么如何将String arg 从MyTest 类传递给Screenshot 类?

【问题讨论】:

    标签: java unit-testing junit junit4


    【解决方案1】:

    TestWatchman 签名不允许您传递任何其他信息,因此您必须使用您所拥有的信息。

    例如,FrameworkMethod 类具有获取单元测试的 java.lang.reflect.Method 对象的方法,因此您可以在屏幕截图中使用该 Method 对象的名称。这不是你想要的,但总比没有好。

    您也可以在Method 上调用getDeclaringClass(),以获取代表测试类的Class,但您将无法获取该类的实例,这会很有用。

    附:我不确定@Override 与您的问题有什么关系。有什么关系?

    【讨论】:

    • 谢谢,最后我用了getName()方法给我的截图起了一些名字。
    猜你喜欢
    • 1970-01-01
    • 2016-01-20
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    相关资源
    最近更新 更多