2

我是单元测试的初学者。我使用 JUnit 和 Mockito。这是我要测试的一种示例方法。

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
}

我尝试了很多方法,但我都做不好。例子:

@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());
}

先感谢您。

4

2 回答 2

3

我不会嘲笑所有可能的事情。我只会模拟您要测试的类并假设 List 行为正确。

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());
    }
}

印刷

[Mock for Aggregate, hashCode: 2037567902, Mock for Person, hashCode: 1629493852, Mock for Aggregate, hashCode: 44220373, Mock for Person, hashCode: 182467149]
size: 4
于 2012-09-17T12:25:21.113 回答
3

您需要问自己,您要测试的是什么?在示例函数中,您要检查当函数给出聚合列表时,它返回聚合和人员的混合扁平列表。

所以,我不会模拟列表,而是检查结果列表是否包含所有预期的项目:

Aggregate aggregate = new Aggregate(...);
//add persons to aggregate.

List<Aggregate<Person>> aggregateList = new ArrayList();
aggregateList.add(aggregate);

List<Person> list = testedClass.getPeopleList(aggregateList);
assertEquals(Collections.singletonList(aggregate), result);

否则,如果您更改迭代参数 List 中所有项目的方式,则需要更新测试。

您可以测试的另一件事:

  1. 作为参数给出的列表不会被修改。
  2. 函数检查null参数。

    Preconditions.checkNotNull(aggregateList, "AggregateList cannot be null");
    
  3. 空参数导致空结果。

于 2012-09-17T12:25:30.233 回答