vendredi 26 juin 2015

Does it make sense to Unittest wrapper methods

This question is somewhat philosophical. Given i have a Method like this:

public List<String> getStuffByName(@NotNull String name) throws SomeException {
        return someDependency.createQuery().byName(name).list().stream()
                .map(execution -> execution.getProcessInstanceId())
                .collect(Collectors.toList());
}

Basically all it does is call a dependencies method and then process it using the stream api.

A Unittest - in a strict understanding(?) - is only testing one isolated unit. So i would Mock the dependencies, as i assume they also are already tested units themselves.

If I go through with this, i end up with a Method that exclusively consists of stuff that is tested elsewhere.

Eg my JMockit Test would look like this:

public void test_get_processes_for_bkey_2(@Mocked ExecutionQuery query,
                                              @Mocked List<String> processes,
                                              @Mocked List<Execution> executions,
                                              @Mocked Stream<Execution> e_stream,
                                              @Mocked Stream<String> pid_stream,
                                              @Mocked Stream<String> pdef_stream

    ) {
    new Expectations() {
        {
            someDependency.createQuery(); result = query;
            query.byName("somefilter"); result = query;
            query.list(); result = executions;
            executions.stream(); result = e_stream;
            e_stream.map((Function) any); result = fin_stream;
            fin_stream.collect((Collector) any); result = processes;
            processes.size(); result = 2;
        }
    };

    assertEquals(tested.getStuffByName("somefilter").size(), 2);
}

But what does this test really tell me?

In test-driven-development, would i omit tests of such "wrapper" methods?

What would a professional approach to test this be, independent of Jmockit or other frameworks?

Aucun commentaire:

Enregistrer un commentaire