This guide walks you through testing an Android weather app under failure conditions—when the network drops, GPS is unavailable, or the weather API returns errors. You’ll learn how to simulate these failures, verify graceful degradation, and ensure the app handles errors without crashing. No special hardware is needed, just Android Studio and a device or emulator.
What You’ll Need
- Android Studio (latest stable version)
- An Android device (with developer options enabled) or an emulator
- The weather app’s source code (or a debug APK with test hooks)
- ADB (Android Debug Bridge) installed (comes with Android Studio)
- Optional: a proxy tool like Charles or Fiddler for API failure simulation
Step-by-Step
- Enable Developer Options and USB Debugging – On your physical device, go to Settings → About Phone and tap Build Number 7 times. Then enable USB Debugging in Settings → Developer Options. For an emulator, this is already enabled.
- Connect the Device – Plug in your device via USB and accept the debugging prompt. Verify the connection by running
adb devicesin a terminal. You should see your device listed as “device”. - Install the App – Build the app in Android Studio and install it on the device/emulator using the Run button or
adb install. - Test Network Failure (Wi-Fi/Data Off) – Turn off Wi-Fi and mobile data on the device. Launch the app. Observe: Does it show a cached weather report? Does it display an error message? Does it crash? Note the behavior.
- Test Network Failure via Airplane Mode – Enable Airplane Mode, then launch the app. This simulates a complete loss of all connectivity. Check that the app handles this state without freezing.
- Simulate GPS Failure – In Developer Options, find “Select mock location app” and set it to a dummy app (or use the emulator’s Location controls). Send a fake location far away (e.g., another country). Launch the app and see if it requests location updates and what happens when the GPS signal is weak.
- Test with Location Off – Turn off Location services entirely (Settings → Location → Off). Launch the app. It should either prompt you to enable location or fall back to a default city. Verify it doesn’t crash.
- Simulate API Failure (Server Error) – Use a proxy tool like Charles to intercept the API requests. Set the proxy on your device to point to your computer. In Charles, enable “Map Remote” or “Breakpoints” to return a 500 error or a malformed JSON response. Launch the app and observe how it handles the error.
- Test with Invalid API Data – Instead of a server error, return valid JSON but with missing fields (e.g., no temperature). Check if the app crashes or shows null values. Ideally, it should display a generic error.
- Test with Timeout – Use Charles to delay the API response by 30 seconds or more. The app should eventually show a timeout error, not hang indefinitely.
- Test with No GPS but Network Available – Turn off location but keep Wi-Fi on. The app should still fetch weather for a default location or ask you to choose one manually.
- Test with GPS but No Network – Turn on location but turn off Wi-Fi/data. The app should use the GPS coordinates but fail to fetch weather, showing an error like “No internet connection.”
- Test Background/Resume – While the app is in a failure state, press Home and then reopen the app. Ensure it doesn’t crash on resume and that error states are handled.
- Test Rapid Toggling – Toggle Wi-Fi on/off quickly while the app is loading. This can expose race conditions or crashes.
- Check Logcat – During all tests, monitor the Logcat window in Android Studio for any unhandled exceptions or warnings. Note any error messages that could indicate poor error handling.
Common Mistakes
- Forgetting to test on a real device – Emulators simulate network conditions differently; always test on at least one physical device.
- Not clearing app data between tests – Cached data can mask failures. Clear the app’s cache/data before each test to start fresh.
- Ignoring the UI state – An error message that appears and then disappears quickly is still a failure. Watch carefully.
- Assuming GPS is the only location source – The app might use network location as a fallback. Test with both GPS and network location disabled.
- Not testing the app’s first launch – The first launch after install is when most permission and network issues surface.
Tips
- Use the emulator’s extended controls – The emulator lets you simulate network speed, latency, and even a “bad network” profile. Go to Extended Controls → Network and select “Full” or “Edge” to simulate slow connections.
- Automate with UI tests – Write Espresso tests that mock the API and location services to test failure scenarios programmatically.
- Include a “last updated” timestamp – When the network fails, the app should show stale data with a timestamp so users know it’s not current.
- Add a retry button – A common best practice is to show a retry button when the API fails. Test that it works when connectivity is restored.
- Use the Android Profiler – Check CPU and memory usage during failures to detect leaks or excessive retries.
Troubleshooting
- App crashes immediately when network is off – Check if the app is trying to access a network resource without checking connectivity. Look for a NullPointerException in Logcat. The fix is to add a network check before making API calls.
- App shows a blank screen instead of an error – This often means the API response is being parsed incorrectly. Use Charles to see the actual response and verify the parsing logic handles errors.
- GPS location is never received – Make sure the app has the ACCESS_FINE_LOCATION permission and that you’ve granted it. Also, in the emulator, send a location manually via Extended Controls → Location.
- Proxy not intercepting traffic – Ensure the device and computer are on the same network, and that the proxy is set correctly in Wi-Fi settings. Also, disable any VPN or firewall that might block it.
- App works on emulator but not on device – This is often a network issue. Check if the device has a valid internet connection and that the API endpoint is reachable from the device’s network.
Frequently Asked Questions
How do I simulate a slow network without a proxy?
Use the emulator’s Network settings to set a low bandwidth profile like “Edge” (2G). This simulates a slow connection and can trigger timeouts.
What should the app do when GPS is off?
It should either prompt the user to enable location or fall back to a manually selected or default city. It should not crash or show a blank screen.
How can I test API failure without a proxy tool?
You can modify the app’s build configuration to point to a mock server that returns errors, or use a library like MockWebServer in your test suite.
Is it necessary to test on a physical device?
Yes, because GPS and network hardware behave differently than emulators. Physical devices also have real-world signal variations that emulators can’t replicate.
What is the best way to handle a timeout in the app?
Set a reasonable timeout (e.g., 10 seconds) and show a user-friendly message like “Request timed out. Please try again.” Provide a retry button.
How do I verify that the app uses cached data?
Load the app with a network connection, then turn off the network and refresh. If it shows the same data with a “Last updated” timestamp, caching is working.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Windows 10 New PC Settings Tweaks
- Flutter vs Android Studio: Key Differences
- VPN on Samsung Galaxy: App & Settings Setup
- How to Assign a Ringtone to a Specific Contact on Android
- How to Send Confetti on iPhone (iMessage Effect)
- iPad Location Not Available? Fix It Now