stumped
stumped

Reputation: 489

Using @Override in JUnit4

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 {  
    @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?

Upvotes: 2

Views: 538

Answers (1)

skaffman
skaffman

Reputation: 403551

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?

Upvotes: 1

Related Questions