简体   繁体   中英

Create a mocked list with objects

I want to create a JUnit test with tests mocked objects:

public class BinCountryCheckFilterImplTest {

    private RiskFilterService riskFilterService = null;

    @Before
    public void beforeEachTest() {

        List<RiskFilters> list = new ArrayList<RiskFilters>();

        riskFilterService = Mockito.mock(RiskFilterService.class);

        // put here list of List<RiskFilters> and return it 
    }

    @Test
    public void testBinCountryCheckFilterImpl() {

        List<RiskFilters> filter_list = riskFilterService.findRiskFiltersByTerminalIdAndType(11, "test");
         // do something                
    }    
}

How I can return the list List<RiskFilters> when RiskFilterService is calle?

Second attempt:

public class BinCountryCheckFilterImplTest {

    private RiskFilterService riskFilterService = null;

    @Mock
    List<RiskFilters> mockList = new ArrayList<RiskFilters>();

    @BeforeClass
    public void beforeEachTest() {

        //if we don't call below, we will get NullPointerException
        MockitoAnnotations.initMocks(this);

        mockList.add(new RiskFilters());

        riskFilterService = Mockito.mock(RiskFilterService.class);
    }

    @Test
    public void testBinCountryCheckFilterImpl() {

        when(riskFilterService.findRiskFiltersByTerminalIdAndType(anyInt(), anyString())).thenReturn(mockList);

        List<RiskFilters> filter_list = riskFilterService.findRiskFiltersByTerminalIdAndType(11, "BinCountryCheckFilter");

    }
}

But I get NPE for riskFilterService . Looks like the method with annotation @test is called before @BeforeClass.

Well you provided very less information. BUt let me put through

you must be having BinCountryCheckFilter class. Please Initialise it in your test class and add annotation @InjectMocks

@InjectMock
private BinCountryCheckFilter binCountryCheckFilter;

take riskFilterService = Mockito.mock(RiskFilterService.class); out of @BeforeClass and put it openly.

But this will just mock your class and will not test anything. One thing you can test is no of calls made. See below

verify(mockList, times(1)).yourMethodName(); 

or add following in your test or before method

when(riskFilterService.yurMethodName).thenReturn(your Return value);

This way you will be able to mock the data you want. Let me know if any other clarity needed.

I am not sure of your JUnit version but you can remove

comelete @BeforeClass from your code now and

 @Mock
    List<RiskFilters> mockList = new ArrayList<RiskFilters>();

too.

When a List or any other Collection is required in a unit test, the first question to ask yourself is: should I create a mock for it, or should I create a List or a Collection containing mocks.

When the logic being tested is not using the list, but just passing the list than you can mock it.

Otherwise it is usually better not to mock a List or a Collection but to create a normal one containing mocked objects because it can become very difficult to know which methods of the List or Collections need to be stubbed. Which methods are called when using a for loop to iterate the items, when using an iterator on them, when using a stream on them, ... ? I often use Collections.singletonList or Arrays.asList with mocked parameters to initialise lists when writing unit tests.

I see that you mock the list and then you call the add method to add data to it while setting up the test. It doesn't make sense to add data to a mocked list. You can use Mockito.when to return it when it should be returned, but then you would get in trouble because you might need to stub more methods and it would be hard to know which ones (isEmpty, size, ...). That you are adding a dataobject to list probably means the method being tested is not just passing the list but will access the data in it. In that case, don't mock the list, but mock the data objects which you put in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM