简体   繁体   English

覆盖JUnit注释?

[英]Override a JUnit annotation?

I have a method I call in my superclass like so: 我有一个我在超类中调用的方法,如下所示:

public class superClass {
  @Before
  void createDriver(){
    driverFactory = new FirefoxDriverFactory();
  }
  ...
}

However, in my test suite specifically which inherits from the super class, I want it so that I can run my test with the @BeforeClass annotation and not the @Before annotation. 但是,在我的测试套件中,特别是继承自超类的测试套件,我想要它,以便我可以使用@BeforeClass注释而不是@Before注释来运行我的测试。 I'm not sure, but is it possible to override the @Before annotation or not? 我不确定,但是否可以覆盖@Before注释? I tried the following: 我尝试了以下方法:

public class derivedClass extends superClass{
  @Override
  @BeforeClass
  void createDriver(){
    driverFactory = new FirefoxDriverFactory();
  }
}

But I get an error that says that the createDriver() method must be static and I must remove the @Override annotation. 但是我得到一个错误,指出createDriver()方法必须是静态的,我必须删除@Override注释。 Is it possible to Override an annotation as opposed to a function? 是否可以覆盖注释而不是函数? I was just wondering. 我只是想知道。 Thanks! 谢谢!

The issue is not in annotation overrides. 问题不在于注释覆盖。 JUnit requires @BeforeClass method to be static: JUnit要求@BeforeClass方法是静态的:

public class derivedClass extends superClass{

  @BeforeClass
  public static void beforeClass(){
   driverFactory = new FirefoxDriverFactory();
  }
}

I think this is what you might be looking for... 我想这就是你可能在寻找的......

public class superClass {
  @Before
  void createDriver(){
    driverFactory = new FirefoxDriverFactory();
  }
  ...
}

public class derivedClass extends superClass{
  @Override
  void createDriver(){
    driverFactory = new FirefoxDriverFactory();
  }
}

JUnit should find superClass.createDriver() via the annotation. JUnit应该通过注释找到superClass.createDriver() However, when this method is invoked on an instance of derivedClass the actual method that should be run is derivedClass.createDriver() since this method overloads the other. 但是,当在derivedClass的实例上调用此方法时,应该运行的实际方法是derivedClass.createDriver()因为此方法会重载另一个。 Try it and let us know. 试一试,告诉我们。

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

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