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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:43:46+00:00 2026-06-11T11:43:46+00:00

I am beginer in unit testing. I use JUnit and Mockito. This is one

  • 0

I am beginer in unit testing.
I use JUnit and Mockito. This is one example method which I want to test.

public List<Person> getPeopleList(List<Aggregate<Person>> aggregateList) {
    List<Person> resultList = new ArrayList<Person>();
    for (Aggregate<Person> aggregate : aggregateList) {
        resultList.add(aggregate);

        for (Person person : aggregate) {
            resultList.add(person);
        }
    }
    return resultList; // the result is person and aggregate list
}

I tried to many ways, but I can’t do it well. Example:

@Test
public void getPeopleListTest(){
    ClassUnderTest testedClass = new ClassUnderTest();

    Aggregate aggregate = mock(Aggregate.class);
    Iterator<Aggregate<Person>> aggregateIterator = mock(Iterator.class);
    when(aggregateIterator.hasNext()).thenReturn(true, false);
    when(aggregateIterator.next()).thenReturn(aggregate);

    List<Aggregate<Person>> aggregateList = mock(List.class);

    aggregateList.add(aggregate);

    List<Person> list = testedClass.getPeopleList(aggregateList);

    assertEquals(1, list.size());
}

Thank you in advance.

  • 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-11T11:43:47+00:00Added an answer on June 11, 2026 at 11:43 am

    I wouldn’t mock every possible thing. I would only mock the class you want to test and assume List behaves correctly.

    public class Main {
        interface Person {
        }
    
        interface Aggregate<T> extends Person, Iterable<T> {
        }
    
        public static List<Person> getPeopleList(List<Aggregate<Person>> aggregateList) {
            List<Person> resultList = new ArrayList<Person>();
            for (Aggregate<Person> aggregate : aggregateList) {
                resultList.add(aggregate);
    
                for (Person person : aggregate) {
                    resultList.add(person);
                }
            }
            return resultList; // the result is person and aggregate list
        }
    
        public static void main(String... args) {
            Aggregate<Person> aggregate = mock(Aggregate.class);
            Aggregate<Person> aggregate2 = mock(Aggregate.class);
            Person person = mock(Person.class);
            Person person2 = mock(Person.class);
            when(aggregate.iterator()).thenReturn(Arrays.asList(person).iterator());
            when(aggregate2.iterator()).thenReturn(Arrays.asList(person2).iterator());
    
            List<Person> list = getPeopleList(
                    Arrays.asList(aggregate, aggregate2));
            System.out.println(list);
            System.out.println("size: " + list.size());
        }
    }
    

    prints

    [Mock for Aggregate, hashCode: 2037567902, Mock for Person, hashCode: 1629493852, Mock for Aggregate, hashCode: 44220373, Mock for Person, hashCode: 182467149]
    size: 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm beginner in testing.I need to create unit test for method with NetworkStream like
I am beginner of unit testing so I want to ask what kind of
I a unit-testing beginner and seem to be stuck on how to unit test
I'm beginner at Unit Testing. Please help me I have class UserData UserData.cs public
Excuse me if this is a silly question but i'm a beginer here. I
Beginner help needed :) I am doign an example form a php book which
Python beginner here, I have a list of lists and want to refer to
I beginer programmer,and don't have any QA experience (only simple test that i write
I beginer in zend framework. ob_start(ob_gzhandler) - why the function not use directly in
Beginner here See - http://www.w3schools.com/js/tryit.asp?filename=try_dom_tablerow_cells In this example, when change content is pressed, new

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.