Java 在JUnit4中使用@Override
我正在使用Selenium RC和JUnit编写测试。如果Java 在JUnit4中使用@Override,java,unit-testing,junit,junit4,Java,Unit Testing,Junit,Junit4,我正在使用Selenium RC和JUnit编写测试。如果@Test失败,我想截屏,但我想使用从Test类传递的特定截屏名称 目前我有: @Rule public ScreenshotRule email = new ScreenshotRule(); @Test public void testLogin() throws InterruptedException { MyTest someTest = new MyTest(selenium); som
@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.
}
以下是屏幕截图
类:
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
}
}
那么如何将
MyTest
类中的String
arg传递给屏幕截图
类 TestWatchman签名不允许您传递任何附加信息,因此您必须使用现有信息
例如,FrameworkMethod
类具有获取单元测试的java.lang.reflect.Method
对象的方法,因此您可以在屏幕截图中使用该Method
对象的名称。这不是你想要的,但总比没有好
您还可以在方法
上调用getDeclaringClass()
,以获取表示测试类的类
,但您将无法获取该类的实例,这将非常有用
另外,我不确定你的问题与@Override
有什么关系。这有什么关系?谢谢,我最后使用了getName()方法为我的屏幕截图命名。