简体   繁体   中英

Mockito in JUnit

I am testing a method that uses Spring API and I need to test it. Is there any way to test it using Mockito ?

Method under test:

@Override
protected List<MonitoringWidget> handleMonitor() {
    IConcurrentMonitoringWidgetAggregator a = (IConcurrentMonitoringWidgetAggregator)AppContext.getBean( Constants.SPRING_BEAN_MONITOR_AGGREGATOR );
    return a.aggregate();
}

You should inject IConcurrentMonitoringWidgetAggregator into the class that has this method. That way, your test case can create an instance of the class under test, passing in a mock IConcurrentMonitoringWidgetAggregator

Note that if aggregate() is fast and the result is easy to verify, it might make sense for your test to pass in a real instance of IConcurrentMonitoringWidgetAggregator (ie test the result of the call, not the interaction).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM