Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Use one JUnit test suite for two alternative class implementations

807591Jun 18 2008 — edited Jun 18 2008
Hi. Newbie to Java here, but some experience in other languages.

I have two classes with the same methods, which are basically alternative implementations of the same requirement. I have a JUnit test suite, which can be used on either by doing a global replace of the class name. Isn't there a better way of doing this? I want to retype the name of the particular class I'm testing in one place and have all the references in the test suite pick up the right class - at compile time.

I've tried searching the net but the terms are too vague - class reference type variable ...

Say my classes are called MyClass1 and MyClass2. The test suite looks like this:
public class MyClassTest extends TestCase {

	private MyClass1 c;

	...

	public void test1() {
		c = new MyClass1(var1, true);
		...	
	}

	...

	public void test2() {
		c = new MyClass1(var2, true);
		...	
	}

	...

	public void test3() {
		c = new MyClass1(var3, true);
		...	
	}
	
	...

}
I've tried inserting a subclass
	private class MyClassRef extends MyClass1 {
		private MyClassRef(String param1, boolean param2) {
			super(param1, param2);
		}
	}
and using MyClassRef in the rest of the test suite - but because I don't have a parameterless constructor I need to give it an explicit constructor. That works ... only one place to change the name of the class under test. But now I want to make the classes into singletons and hide the constructor. This gets worse!

In Delphi I could just say
  type MyClassRef = MyClass1; 
and the compiler would effectively make MyClassRef a synonym for MyClass1. Is there any equivalent in Java?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 16 2008
Added on Jun 18 2008
7 comments
332 views