开发者

Using @Override in JUnit4

开发者 https://www.devze.com 2023-02-21 04:43 出处:网络
I am using Selenium RC and JUnit to write tests. 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.

I am using Selenium RC and JUnit to write tests. 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.

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 :

public class ScreenshotRule extends TestWatchman {  
  开发者_Go百科  @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?


The TestWatchman signature does not permit you to pass any additional information, so you'll have to work with what you have.

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

P.S. I'm not sure what @Override has to do with your question. How is it relevant?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号