3

Suppose we have multiple test suites:

@RunWith(Suite.class)               
@Suite.SuiteClasses({               
  Tests1.class,
  Tests2.class,             
})      
public class TestSuite1 {               
}

@RunWith(Suite.class)               
@Suite.SuiteClasses({               
  Tests3.class,
  Tests4.class,             
})      
public class TestSuite2 {               
}

Would be possible to add an @AfterClass tear down with a flexible way without needing to add the same block of code on all the TestSuite* classes?

@AfterClass
public static void baseTearDown() {
  // Close some resourcess
}
Dio
  • 684
  • 2
  • 9
  • 18
  • You can write a parent class for all of your suites with an afterclass method – gkgkgkgk Aug 10 '18 at 14:49
  • You could create an utility class, but what _with a flexible_ mean?? – lealceldeiro Aug 10 '18 at 14:49
  • @gkgkgkgk in my experience, for big projects that can be cumbersome to maintain. – lealceldeiro Aug 10 '18 at 14:50
  • I was wondering if we could use some Guice magic to avoid the manual edit of "extends ...". – Dio Aug 10 '18 at 14:53
  • @lealceldeiro how so? Not disagreeing, just curious. – gkgkgkgk Aug 10 '18 at 14:55
  • @gkgkgkgk if you set up a `tearDow` logic in a base class and use it as a parent class for a lot of subclasses... when in the future some minor change occurs and you need to modify the `tearDown` logic, it might apply for some cases (of the base class), but not for all of them. This is when the chaos begins, since it can be really hard to track where the failures are, base class? specific unit test? and the list continues. – lealceldeiro Aug 10 '18 at 15:03
  • @gkgkgkgk you might want to take a look at https://www.petrikainulainen.net/programming/unit-testing/3-reasons-why-we-should-not-use-inheritance-in-our-tests/ , https://stackoverflow.com/questions/6715281/how-to-avoid-inheritance-in-junit-test-cases – lealceldeiro Aug 10 '18 at 15:04
  • 1
    @lealceldeiro thanks for the info! Those were both good reads. good to know – gkgkgkgk Aug 10 '18 at 15:11

0 Answers0