开发者

JUnit : Is there a way to skip a test belonging to a Test class's parent?

开发者 https://www.devze.com 2023-02-09 19:29 出处:网络
I have two classes: public abstract class AbstractFoobar { ... } and public class ConcreteFoobar extends AbstractFoobar { ... }

I have two classes:

public abstract class AbstractFoobar { ... }

and

public class ConcreteFoobar extends AbstractFoobar { ... }  

I have corresponding test classes for these two classes:

public class AbstractFoobarTest { ... }

and

public class ConcreteFoobarTest extends AbstractFoobarTest { ... }

When I run ConcreteFoobarTest (in JUnit), the annotated @Test methods in AbstractFoobarTest get run along with those d开发者_高级运维eclared directly on ConcreteFoobarTest because they are inherited.

Is there anyway to skip them?


Update: Misunderstood the Question

  1. Make AbstractFoobarTest abstract. That way the test methods in AbstractFoobarTest are only run once, when ConcreteFoobarTest is executed. You are going to need a concrete subclass to test the methods in AbstractFoobar anyway.
  2. Or just remove the inheritance between AbstractFoobartest and ConcreteFoobarTest. You don't want to make use of it anyway.


You really don't need AbstractFoobarTest in the first place because after all you can't create an instance of the abstract class, so, you will still rely on your concrete class to test your abstract class.

That means, you will end up using your ConcreteFoobarTest will test the APIs from the abstract class. Thus, you will have just this:-

public class ConcreteFoobarTest  { ... 
   @Test
   public void testOne() {
      ConcreteFoobar cf = new ConcreteFoobar();
      // assert cf.conreteAPI();
   }

   @Test
   public void testTwo() {
      ConcreteFoobar cf = new ConcreteFoobar();
      // assert cf.abstractClassAPI();
   }
}


Using process tags would allow you to run into specific bodies of code

0

精彩评论

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