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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:53:41+00:00 2026-06-13T13:53:41+00:00

I am interested in getting ideas on how can I design my junit test

  • 0

I am interested in getting ideas on how can I design my junit test cases so that they are more readable and maintainable. Here is the problem – In all my junit test cases I have some predefined steps to be executed like below

public class MyTestClass {

private Service myService;

@Test
public void testCase1() {
    mockObjectX("A");
    mockObjectY(1);
    mockObjectZ("M", 1);

    myService.validate("value1", "value2");

}

@Test
public void testCase2() {
    mockObjectX("Q");
    mockObjectY(5);
    mockObjectZ("Z", 10);

    myService.validate("value3", "value4");

}


private void mockObjectZ(String value1, int value2) {
    //mock object Z
}

private void mockObjectY(Integer value) {
    //mock object Y
}

private void mockObjectX(String value) {
    //mock object X
}
}

The issue is if I were to add a new test case I need make sure that the new test case calls mockObjectX(), mockObjectY(), mockObjectZ(). Is there a better way (a more maintainable way) of designing my test cases such that adding new testcases becomes simpler?

I think that @Before cannot be used since the mock* methods accept parameters.

One way I could think of is having a class containing methods for each parameter value required to mock object X, Y and Z

public class MyTestClass {

private Service myService;

@Test
public void testCase1() {
    mockObjects(new TestCase1());
    myService.validate("value1", "value2");

}

@Test
public void testCase2() {
    mockObjects(new TestCase2());
    myService.validate("value3", "value4");

}

private void mockObjects(ServiceObjectMocker serviceObjectMocker) {
    //mock object x, y and z by calling respective methods from serviceObjectMocker   
}  

private class ServiceObjectMocker {

    protected Object[] getValuesForObjectZ() {
        //pass default values
        return new Object[] {};     
    }

    protected Integer getValuesForObjectY() {
        //pass default values
        return 1;
    }

    protected String getValuesForObjectX() {
        //pass default values
        return "A";
    }

}

private class TestCase1 extends ServiceObjectMocker {
    @Override
    protected String getValuesForObjectX() {
        return "B";    
    }  
}

private class TestCase2 extends ServiceObjectMocker {
    @Override
    protected String getValuesForObjectX() {
        //pass different values
        return "Q";
    }

    @Override
    protected Integer getValuesForObjectY() {
        //pass default values
        return 10;
    }

}
}

This reduces the no. of method calls each test case needs to make. Any better ideas?

  • 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-13T13:53:41+00:00Added an answer on June 13, 2026 at 1:53 pm

    If you really area doing the same thing each time, it’s just the data that varies, then look at @Parameterized.

    You could then have something like (untested and uncompiled):

    @RunWith(Parameterized.class)
    public class MyTestClass {
      @Parameters
      public static List<Object[]> data() {
          return Arrays.asList(new Object[][] {
              { "A", 1, "M", 1, "value1", "value2" },
              { "Q", 5, "Z", 10, "value3", "value4" },
          });
      }
    
      private String xValue;
      private int yValue;
      private String zValueString;
      private int zValueInt;
      private String expected1;
      private String expected2;
    
      public MyTestClass(String xValue, int yValue, String zValueString, int zValueInt, String expected1, String expected2) {
        this.xValue = xValue;
        this.yValue = yValue;
        this.zValueString = zValueString;
        this.zValueInt = zValueInt;
        this.expected1 = expected1;
        this.expected2 = expected2;
      }
    
      @Test
      public void test() {
        mockObjectX(xValue);
        mockObjectY(yValue);
        mockObjectZ(zValueString, zValueInt);
    
        myService.validate(expected1, expected2);
      }
    }
    

    You are then only defining your test once.

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

Sidebar

Related Questions

An easy one that I am interested in getting your thoughts on: With your
I have trying to get around making my first C# application(that can do more
I'm interested in building a python script that can give me stats on how
I have an array of 6 ints that I am interested in getting a
I am interested into getting into bash scripting and would like to know how
I'm interested in getting the SQLite database containing browsing history from the built in
I have a multidimensional array, I am interested in getting all the elements (one
Probably a bit off topic question, but it's something I'm really interested in getting
I'm having trouble getting Spring.Net to log, using Log4Net. I'm particulary interested in seeing
im interested how many api calls per second or per minute i can do

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.