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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:32:28+00:00 2026-05-22T21:32:28+00:00

I would like to test an API, which received one argument and returns a

  • 0

I would like to test an API, which received one argument and returns a set. The test invokes the API with an argument and checks if the returned set contains expected values.

Suppose, I have to test the API with arguments arg1, arg2, and arg3 and check if values a, b, c appear in the returned set. That is, my test case looks as follows:

  • invoke the API with arg1 and check if a,
    b, c appear in the returned set.
  • invoke the API with arg2 and check if a,
    b, c appear in the returned set.
  • invoke the API with arg3 and check if a,
    b, c appear in the returned set.

How to develop this test case with Junit 4 ? What if I have to add arg4 ? What if I have to check if value d appear in the returned set ? Can I read the list of arguments and expected values from the configuration?

  • 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-05-22T21:32:29+00:00Added an answer on May 22, 2026 at 9:32 pm

    Fluent assertions

    First of all, use FEST-Assertions library to introduce pleasantly looking assertions with meaningful error messages:

    assertThat(method(arg1)).containsExactly(a, b, c);
    assertThat(method(arg2)).containsExactly(a, b, c);
    assertThat(method(arg3)).containsExactly(a, b, c);
    

    The BDD way

    But I understand your question is not about the syntax, but about methodology: what should you do if arg4 needs to be tested? Well, if arg1 through arg4 have a different semantic meaning, I would advice you to have a separate test for each argument. Very verbose, but also extremely readable (pseudocode):

    @Test
    public void shouldReturnAbcWhenSomeArgumentUsed() {
      //given
      Object arg = arg1;
    
      //when
      Set<Object> result = method(arg);
    
      //then
      assertThat(result).containsExactly(a, b, c);
    }
    

    ..and repeat this for every test. The key part is: method name should represent the meaning of an argument, what does this method actually test, what do you expect, what is the scenario?

    Consider testing isEven method. I would recommend the following tests:

    • shouldReturnTrueForZero
    • shouldReturnTrueForSmallPositiveEvenNumber
    • shouldReturnTrueForLargePositiveEvenNumber
    • shouldReturnFalseForSmallPositiveOddNumber
    • shouldReturnFalseForLargePositiveOddNumber
    • … and mirror the tests for negative numbers

    Each test represent a slightly different, well defined scenario. On the other hand you might generate literally thousands of shouldReturnFalseWhen227, but what is the value of such a test suite, except it’s large? Test semantically different arguments and corner cases, defining precisesly what case is being tested.

    Parameterized way

    If you really want to have just a single generic test method, Parameterized runner is the way to go. I think the example is self-explanatory. NB: you can parameterize expected values as well.

    @RunWith(value = Parameterized.class)
    public class JunitTest6 {
    
        private Object arg;
    
        public JunitTest6(Object arg) {
            this.arg = arg;
        }
    
        @Parameterized.Parameters
        public static Collection<Object[]> data() {
            return Arrays.asList(
                    new Object[][]{
                            {arg1},
                            {arg2},
                            {arg3}
                    });
        }
    
        @Test
        public void testMethod() {
            assertThat(method(arg)).containsExcatly(a, b, c);
        }
    
    }
    

    Based on this.

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

Sidebar

Related Questions

I would like to test a function with a tuple from a set of
I am starting out with automated testing and I would like to test one
I would like to unit test a DEPENDENT service layer which allows me to
I would like to create a small test app for iOS in which I
I would like to test the Flickr API using Matlab. There is no Flickr
I would like to test a string containing a path to a file for
I would like to test if a point is within a particular distance of
I would like to test some exception handling logic in the empty catch block
I would like to test an app that uses the Clipboard (WindowsForms) and I
I've nearly finished the development of a project and would like to test its

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.