简体   繁体   中英

Implementing Test instead of extending TestCase in Junit [Java]

I have a class C which must extend a class N because it provides functionality I must have. I want C to be a JUnit test so I want to extend TestCase, because I am using a test executor, a Junit suite object, which requires tests being added to be it to be of type TestCase. This means that I have to use the TestCase extension.

Java, however, does not allow for multiple inheritance, so I need to, instead, use an interface. Is there an existing interface? Or is there a way to dynamically execute Junit tests that don't extend from TestCase?

I don't extend Test or TestCase anymore. I use the @Test annotations since version 4.4.

Do you really need your C class to extend N? Can't you just opt for composition instead of extension? Your C could have N as internal attribute and be using the functionalities provided by it. Just an idea.

The interface is called Test , see: http://junit.sourceforge.net/junit3.8.1/javadoc/junit/framework/Test.html

However, in newer versions of JUnit you should definitely go for the @Test attribute and add your test classes to your suite by specifying the type via YourTestClass.class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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