简体   繁体   English

在JUnit4中使用@Override

[英]Using @Override in JUnit4

I am using Selenium RC and JUnit to write tests. 我正在使用Selenium RC和JUnit编写测试。 If a @Test fails, I would like to take a screen shot, but I want to use a specific screenshot name which is passed from the Test class. 如果@Test失败,我想截屏,但是我想使用从Test类传递的特定屏幕快照名称。

Currently I have : 目前我有:

@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.
}

Here is the Screenshot 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  
    }  
}

So how do I pass a String arg from the MyTest class to the Screenshot class? 那么,如何将String arg从MyTest类传递给Screenshot类呢?

The TestWatchman signature does not permit you to pass any additional information, so you'll have to work with what you have. TestWatchman签名不允许您传递任何其他信息,因此您必须使用已有的内容。

For example, the FrameworkMethod class has methods to fetch the unit test's java.lang.reflect.Method object, so you could just use the name of that Method object for your screenshot. 例如, FrameworkMethod类具有一些方法来获取单元测试的java.lang.reflect.Method对象,因此您可以在屏幕截图中使用该Method对象的名称。 It's not what you wanted, but it's better than nothing. 这不是您想要的,但总比没有好。

You could also call the getDeclaringClass() on the Method , to fetch the Class representing the test class, but you won't be able to fetch the instance of that class, which would be useful. 您还可以在Method上调用getDeclaringClass()来获取表示测试类的Class ,但是您将无法获取该类的实例 ,这将很有用。

PS I'm not sure what @Override has to do with your question. PS我不确定@Override与您的问题有什么关系。 How is it relevant? 有什么关系?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM