Jon Lorusso
Jon Lorusso

Reputation: 488

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

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 declared directly on ConcreteFoobarTest because they are inherited.

Is there anyway to skip them?

Upvotes: 2

Views: 2637

Answers (3)

sweetiewill
sweetiewill

Reputation: 1

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

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299218

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.

Upvotes: 1

limc
limc

Reputation: 40176

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();
   }
}

Upvotes: 1

Related Questions