How do you call Robolectric's ShadowWifiManager.setScanResults()?

During testing, I need to make the WifiManager.getScanResults() method return a non-empty list. I'm using Robolectric. I found the ShadowWifiManager has a setScanResults() method, that takes a list of ScanResult objects -- but they don't have a public constructor.

Upvotes: 2

Views: 360

Answers (1)

I found that Robolectric has a shadow of ScanResult, also, which has a newInstance method. It can be used like this:

shadowOf(((WifiManager)controller.get().getSystemService(Context.WIFI_SERVICE))).setScanResults(Collections.singletonList(ShadowScanResult.newInstance("Foo", "Bar", "", 1, 2)));

or, more verbosely: final int level = 5; final int frequency = 100; final ScanResult scanResult = ShadowScanResult.newInstance( "A fake SSID", "A fake BSSID", "Some capabilities", level, frequency); final Context context = controller.get(); final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); shadowOf(wifiManager).setScanResults(Collections.singletonList(scanResult));

Upvotes: 2

Related Questions