@PostConstruct is not called when using MockitoAnnotations

@RunWith(MockitoJUnitRunner.class)
public class TestMail{    
    @Autowired(required = true)
    SomeFactory someFactory;
    private @Mock MailService mailService;
    private @Captor ArgumentCaptor<List<MailList>> mailListCaptor;
    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }  
    @PostConstruct
    public void init() throws Exception {
            logger.info("someFactory {}", someFactory);
    }
}

      

This is a sample code. Before using mockito or @Beofre annotation everything seems fine. Now its mocking objects is correct, but someFactory is not auto-validated correctly.

Before Mockito everything worked fine.

+3


source to share


1 answer


If you want SomeFactory dependencies to be injected into it, use the @InjectMocks annotation.

http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/InjectMocks.html



Refer to Mockito: Inject real objects into @Autowired private fields as well

0


source







All Articles