У меня есть простой класс Controller, как показано ниже: -
@RestController
public class CrawlerAppController {
private static final Logger LOGGER = LoggerFactory.getLogger(CrawlerAppController.class);
@Autowired
private CrawlerServiceInterface crawlerService;
/* The response time of the crawling operation is directly proportional to the no of pages
* we want to crawl. Keeping a default value of 10 so we can view the results quicker.
* author: Arunava Paul
*/
@RequestMapping(value = "/crawl", method = { RequestMethod.GET })
public Object crawlUrl(@RequestParam(value = "URL") String URL,
@RequestParam(value = "max", defaultValue = "10") int maxPages) throws Exception {
if(!URL.startsWith("https://"))
URL="https://"+URL;
LOGGER.info("Request Received. Domain "+URL+" Pages to be Crawled "+maxPages);
return crawlerService.crawlService(URL, maxPages);
}
}
Я написал класс Junit, как показано ниже: -
@RunWith(PowerMockRunner.class)
public class CrawlerAppControllerTest {
Object obj=new Object();
@Spy
@InjectMocks
private CrawlerServiceInterface crawlerService = Mockito.any(CrawlerService.class);
@InjectMocks
CrawlerAppController appController = new CrawlerAppController();
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void testController() throws Exception {
when(crawlerService.crawlService("https://vogella.com", 20)).thenReturn(obj);
assertEquals(appController.crawlUrl("vogella.com",20), obj);
}
}
Он всегда входит в класс Service, а оператор when не выполняется.Может кто-то пожалуйста посоветовать, что я сделал не так.Ниже приводится ошибка, если я запускаю Junit.
![enter image description here](https://i.stack.imgur.com/sxLvV.jpg)