Injecting Mockito Mock objects using Spring JavaConfig and @Autowired

前端 未结 3 1097
执笔经年
执笔经年 2021-02-07 22:39

I\'m trying to replace an @Autowired object with a Mockito mock object. The usual way of doing this was with xml using Springockito:



        
相关标签:
3条回答
  • 2021-02-07 22:43

    Moving away from the now unmaintained (as of this writing) Spingockito-annotations and to Mockito, we have a way of doing this very simply:

    @RunWith(MockitoJUnitRunner.class)
    @ContextConfiguration
    public class MyTestClass {
    
        @Mock MockInterface mockObj;
    
        // test code
    }
    

    If you're using a real object, but would like to mock a dependency within it, for instance testing a service layer with DAO:

    @RunWith(MockitoJUnitRunner.class)
    @ContextConfiguration
    public class MyTestClass {
    
        @InjectMocks RealService;
    
        @Mock MockDAO mockDAO;
    
        // test code
    }
    

    Finally, this can also be applied to Spring-boot, but using annotation initialization within setUp() until multiple class runners are supported:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = MyMainSpringBootClass.class)
    public class MyTestClass {
    
        @InjectMocks RealService;
    
        @Mock MockDAO mockDAO;
    
        @Before
        public final void setUp() throws Exception{
            MockitoAnnotations.initMocks(this);
        }
    
        // test code
    }
    
    0 讨论(0)
  • 2021-02-07 22:51

    It appears that SpringockitoContextLoader extends GenericXmlContextLoader which is described as:

    Concrete implementation of AbstractGenericContextLoader that reads bean definitions from XML resources.

    So you are limited to xml bean definitions at the moment.

    You could write your own context loader, taking relevant parts from the SpringockitoContextLoader class. Take a look here to get started, perhaps you could extend AnnotationConfigContextLoader for example?

    0 讨论(0)
  • 2021-02-07 23:07

    Outdated and deprecated!

    Read about mocking and spying in Spring Boot 1.4

    Please read also @ethesx answer, Springockito is unmaintaned

    Old answer

    This is possible now to mock Spring application without any XML file with Springockito-annotations.. This solution works also with Spring Boot.

    import static org.mockito.BDDMockito.*;
    import org.kubek2k.springockito.annotations.*;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = Application.class, 
         loader = SpringockitoAnnotatedContextLoader.class)
    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
    public class MainControllerTest {
    
        @Autowired
        MainController mainController;
    
        @Autowired
        @ReplaceWithMock
        FooService fooService;
    
        @Test
        public void shouldGetBar() {
            //given
            given(fooService.result("foo")).willReturn("bar");
    
            //when
            Bar bar build = fooService.getBar("foo");
    
            //then
            assertThat(bar).isNotNull();
        }
    }
    

    Dependencies: org.kubek2k:springockito-annotations:1.0.9

    0 讨论(0)
提交回复
热议问题