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 8273919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:35:56+00:00 2026-06-08T07:35:56+00:00

I wrote an interface MyInterface , which is going to be implemented by different

  • 0

I wrote an interface MyInterface, which is going to be implemented by different implementors.

I also wrote a class MyInterfaceTest, which contains generic test methods that all implementors should be able to use to test their implementations.

I just don’t know how to make it work as a JUnit test.

Currently, I have something like this:

public class MyInterfaceTest {
    private static MyInterface theImplementationToTest = null;

    @BeforeClass public static void setUpBeforeClass() throws Exception {
                // put your implementation here:
        theImplementationToTest = new Implementation(...);
    }

    @AfterClass public static void tearDownAfterClass() throws Exception { 
        theImplementationToTest = null;
    }

    @Test public void test1() { /* uses theImplementationToTest */ }    
    @Test public void test2() { /* uses theImplementationToTest */ }    
}

I use the static method setUpBeforeClass because the initialization of each implementation takes a lot of time, so I want to initialize it once for all tests.

With this version of the test, implementors have to change the code of setUpBeforeClass and put their own implementation.

I am sure that there is another way to write MyInterfaceTest, so that implementors will only have to inherit it or send it a parameter, and not change the code. However, I am not experienced enough in JUnit to make it work. Can you please show me how to do this?

  • 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-08T07:35:58+00:00Added an answer on June 8, 2026 at 7:35 am

    You can have the subclass implement just the before class method and inherit all the tests.

    import org.junit.*;
    
    public class ImplementingClassTest extends MyInterfaceTest {
    
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
            // put your implementation here:
             theImplementationToTest = new MyInterfaceImpl();
        }
    
    }
    

    This makes the abstract class you are writing look like:

    import org.junit.*;
    
    public abstract class MyInterfaceTest {
        protected static MyInterface theImplementationToTest = null;
    
        @AfterClass
        public static void tearDownAfterClass() throws Exception {
            theImplementationToTest = null;
        }
    
        @Test
        public void test1() { /* uses theImplementationToTest */
        }
    
        @Test
        public void test2() { /* uses theImplementationToTest */
        }
    }
    

    Normally, you would make the method the subclasses needed to implement abstract. Can’t do that here since it is a static setup method. (In addition, you might want to refactor the instantiations to not take a long time as this is often an anti-pattern).

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

Sidebar

Related Questions

How can I enforce that my test class covers a particular interface? Also is
I wrote a class that implements the IGroupPolicyObject Interface in COM. one of the
Consider the following console application: class Program { static void Main() { MyInterface test
I want to use a generic class inside a nested static interface. My objective
I wrote a class to implement the interface Validator,and now how can I get
I'm using a PHP class someone wrote to interface with the BaseCamp API. The
I'm using my own generic abstract class (MyAsyncTask) which extends AsyncTasks, and need a
Using Spring 3.0, I wrote an interface to hide from the application which template
I don't know what is going on.. I did a Repository Generic Pattern Interface
I wrote a simple implementation class: public interface MyRemote extends Remote { public String

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.