I have a class that I am trying to unit test. I am trying to test it with Mockito, and to solve spring injection with mockito I use Springockito and Springockito annotations.
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath:testApplication-context-EU.xml"}) public class RelatedSearchToHotUrlProcessorTest extends AbstractJUnit4SpringContextTests { @Autowired RelatedSearchToHotUrlProcessor processor; @ReplaceWithMock private RestOperations restTemplate; @Test public void testGetCategoryFromIdWithNoStoredAlias() { Taxonomy mockTaxonomy = mock(Taxonomy.class, RETURNS_DEEP_STUBS); GraphType.Node mockNode = mock(GraphType.Node.class); when(restTemplate.getForObject(anyString(), eq(Taxonomy.class))).thenReturn(mockTaxonomy); when(mockTaxonomy .getRev() .get(0) .getCountry() .get(0) .getGraph() .getNodeOrAtom() .get(0)).thenReturn(mockNode); when(mockNode.getAlias()).thenReturn("mockalias"); String categoryAlias = processor.getCategoryAliasFromId(13130L); assertEquals("mockalias", categoryAlias); } }
If I delete the lines @ReplaceWithMock and private RestOperations restTemplate , then it makes the right call and the value can be checked as correct. However, I want to mock the RestOperations object inside the processor, but using @ReplaceWithMock makes the restTemplate variable null, causing it to fail. I was not able to figure out how to isolate this participant and mock him.
source share