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つのこと:

  1. 引数として指定されたリストは変更されません。
  2. 関数はnull引数をチェックします。

    Preconditions.checkNotNull(aggregateList, "AggregateList cannot be null");
    
  3. 引数が空の場合、結果は空になります。

于 2012-09-17T12:25:30.233 に答える