Simple test with Spring services and mocks

This is a sample how to use mocks in a junit test class:

package de.poulter.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SampleTest{

    private static final Logger log = Logger.getLogger(SampleTest.class);

    @Configuration
    static class SpringConfig {

        @Bean
        public FirstService firstService() {
            FirstServiceImpl firstServiceImpl = new FirstServiceImpl();
            firstServiceImpl.setSecondService(secondService());
            
            return firstServiceImpl;
        }

        @Bean
        public SecondService secondService() {
            return Mockito.mock(SecondService.class);
        }
    }

    @Autowired
    FirstService firstService;

    @Autowired
    SecondService secondService;

    @Before    
    public void resetMocks() {
        Mockito.reset(secondService);
    }
    
    @Test
    public void checkServicesTest() {
        assertNotNull(firstService);
        assertNotNull(secondService);
    }
    
    @Test
    public void theTest() {
        
        when(secondService.methodX(any(TypeY.class))).thenAnswer(
            new Answer<TypeX>() {
                @Override
                public TypeX answer(InvocationOnMock invocation) {
                    TypeX typeX = new TypeX();
                    return typeX ;
                }
            }
        );
                
        firstService.toBeTestedMethod(value1, value2);
        
        ArgumentCaptor<TypeY> captor = ArgumentCaptor.forClass(TypeY.class);
        verify(secondService, times(1)).methodX(captor.capture());
        TypeY typeY= captor.getValue();
        
        assertEquals(expected, typeY);
    }
}