-7

Testing class

stream list into map, where I get atribute from an element of this list

public class MyClass {

    private final Map<String, IMyInterface> map;

    public MyClass(final List<IMyInterface> list) {
        this.map = list.stream().collect(Collectors.toMap(IMyInterface::getUniqueName, i -> i));
    }
}

Test

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
    @InjectMock
    private MyClass instance;
    @Spy
    private ArrayList<IMyInterface> list;
    @Mock
    private A a;
    @Mock
    private B b;

    @Before void setUp() throws Exception {
        list.add(a);
        list.add(b);
    }
}

Or Test

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
    @Spy
    private ArrayList<IMyInterface> list;
    @Mock
    private A a;
    @Mock
    private B b;

    private class MockedClass extends MyClass {
        MockedClass(List<IMyInterface> list) {
            super(list);
        }
    }

    @Before void setUp() throws Exception {
        list.add(a);
        list.add(b);
    }
}

How to get injected Map after execute constructor? I have to test this class and use map object

EDIT:

IMyInterface::getUniqueName()

is a method in interface objects A and B implements IMyInterface

I want to collect injected list into map When I add elements into list, I got it in debugging mode in my tested class, but when debugging mode is on

list.stream().collect(Collectors.toMap(IMyInterface::getUniqueName(), i -> i));

it stop

  • You could use *reflection* to somehow extract that map – Lino Jun 15 '18 at 11:32
  • what is it - `IMyInterface::getUniqueName()`? what did you mean by "a lambda method"? – Andrew Tobilko Jun 15 '18 at 11:33
  • It's unclear what you're asking here. If I've understood correctly, your question has *absolutely nothing to do* with your example code and is in fact "how do I read a private field?" in which case your answer is already present in question [1196192](https://stackoverflow.com/questions/1196192/how-to-read-the-value-of-a-private-field-from-a-different-class-in-java). – Paul Benn Jun 15 '18 at 11:34
  • sorry, without `()` in `IMyInterface::getUniqueName()` – Filip Pawlak Jun 15 '18 at 11:46
  • 1
    The real solution is: test the behavior of your class under test. Avoid peaking into internal state. In other words: you pass in a list, and you *know* how the resulting map should look like. Now call some other method on your class under test, and *assert* on something that relies on the map content. In other words: you intend to test an implementation detail. Which, you shouldnt. – GhostCat Jun 15 '18 at 12:04
  • You can use ArgumentCaptor to capture the Map, not sure please check and let me now https://static.javadoc.io/org.mockito/mockito-core/2.6.9/org/mockito/ArgumentCaptor.html – srinij Jun 15 '18 at 12:07

1 Answers1

0

The correct answer is that should I mock A and B in my @Before

private A a;
private B b;


@Before
public void setUp() throws Exception {
  a = new A(mock.Service.class); // cause it have an argument
  b = new B(mock.Service.class); // cause it have an argument
  list.add(a);
  list.add(b);
}