Mockito: Inject real objects into private @Autowired fields

后端 未结 5 2255
孤街浪徒
孤街浪徒 2020-11-29 17:56

I\'m using Mockito\'s @Mock and @InjectMocks annotations to inject dependencies into private fields which are annotated with Spring\'s @Autow

5条回答
  •  猫巷女王i
    2020-11-29 18:22

    I know this is an old question, but we were faced with the same problem when trying to inject Strings. So we invented a JUnit5/Mockito extension that does exactly what you want: https://github.com/exabrial/mockito-object-injection

    EDIT:

    @InjectionMap
     private Map injectionMap = new HashMap<>();
    
     @BeforeEach
     public void beforeEach() throws Exception {
      injectionMap.put("securityEnabled", Boolean.TRUE);
     }
    
     @AfterEach
     public void afterEach() throws Exception {
      injectionMap.clear();
     }
    

提交回复
热议问题