1

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?

2
  • which version of junit are you using? Commented Jul 23, 2013 at 20:10
  • You need to use JUnit4 to do that. Commented Jul 23, 2013 at 20:28

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

Obviously he is not using the new annotations API ;-)
@D.R.: Not obvious to me at all, maybe he is just used to the old way?
I adapted my answer...wasn't sure how somebody could know about unit tests but sleep away years of API evolution *g*
I need to dynamically create JUnit tests during run time. I need a runner object that takes in object instances and executes them; unless there's a way to do that when the instances aren't of type TestCase, etc.
1

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.

2 Comments

If anything, I would first avoid inheritance between the unit tests.
No, I can't compose them; N has protected methods, and I cannot put C in the same package because I didn't write N.
0

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

2 Comments

This might be what I'm looking for. Your suggestion abut @Test doesn't work because I'm trying to do something like new Test(x), new Test(y), etc.. I am dynamically creating tests at run time; I can't just specify concrete classes.
If you can't specifiy concrete classes you can dynamically specify them via reflection.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.