Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8138531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:31:06+00:00 2026-06-06T11:31:06+00:00

I am trying to write contract tests for some widely used interfaces: Along the

  • 0

I am trying to write contract tests for some widely used interfaces:

Along the lines of:

public abstract class MyInterfaceContractTest extends TestCase {

  private MyInterface _toTest;

  public void setUp(){
    _toTest = getTestableImplementation();
  }

  protected abstract MyInterface getTestableImplementation();

  public void testContract(){

  }
}

…and…

public class MyInterfaceImplementationTest extends MyInterfaceContractTest {

  protected MyInterface getTestableImplementation(){
    return new MyInterfaceImplementation(...);
  }
}

However, I want to be able to test multiple instances of MyInterfaceImplementation. In my use case, this is an immutable object containing a collection of data (with accessors specified as per the interface MyInterface), and it might be empty, or have a small amount of data, or even lots of data.

So the question is, how can I test multiple instances of my implementations?

At the moment, I have to initialise the implementation to pass it into the abstract contract test. One approach would be to have multiple test classes for each implementation, where each test class tests a particular instance of that implementation – but that then seems a bit voluminous and difficult to keep track of.

FWIW, I’m using JUnit 3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:31:08+00:00Added an answer on June 6, 2026 at 11:31 am

    If I’ve understood your need correctly, you want to run the same test method or methods with multiple implementations of the same interface.

    I don’t know how to do this very nicely in JUnit 3.

    If you’re willing to upgrade to JUnit 4, this can be done by using a parametrized test.

    For the simple example of running a single test method on two implementations of an interface, your test code could look something like this:

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    import static junit.framework.Assert.assertEquals;
    
    // Tell JUnit4 to run it as a parametrized test.
    @RunWith(Parameterized.class)
    public class MyInterfaceContractTest {
    
        private MyInterface _toTest;
    
        // Load the test data into your test instance via constructor
        public MyInterfaceContractTest(MyInterface impl) {
            this._toTest = impl;
        }
    
        // Supply the test data, in this example just the two implementations you want to test.
        @Parameterized.Parameters
        public static Collection<Object[]> generateData() {
            return Arrays.asList(new Object[]{new MyInterfaceImpl1()}, new Object[]{new MyInterfaceImpl2()});
        }
    
        @Test
        public void testContract(){
            // assert whatever, using your _toTest field
        }
    }
    

    On running this test, JUnit will run the test twice, calling the constructor with the successive entries in the parameter list.

    If you have more complex things, like different expectations for the different implementations, the data generation could return lists of Object arrays that contain multiple elements, and the constructor would then take a corresponding multiple arguments.

    If you need to reinitialize the object under test between test methods, you might also want to use the trick I described in this related question.

    I believe similar things are possible in TestNG, which might be another choice for your upgrade path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to write a code at the moment that basically tests to see if
I'm trying to write a function that extends jquery. It appears to be working
I am trying write a script where when a user clicks on a link,
I m trying write code that after reset set up rrpmax as 3000. It
I am trying write a function that generates simulated data but if the simulated
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write out syslog entries containing strings but they don't register. // person.name
Trying to write a chat, like on facebook, I wondered if two clients can
Trying to write a function to see how often an object exists and give

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.