Unveiling the Power of Truth in Black Box Testing with Android and JUnit
Introduction
The motivation of this article is based on a simple question asked by one of my colleague while reviewing my code.
“why do you prefer verify over assertion?”
In the world of making software, testing is like a guardian that checks if the software works well and is trustworthy. Among the different ways to test, there’s one called black box testing. This is when we check how a program works from the outside without looking at how it’s made inside.
For apps on Android phones, one important way to test them is by using something called JUnit. It’s a popular tool for testing. In this article, we will talk about black box testing for Android apps using JUnit. We’ll focus on using the Truth library instead of the usual methods to check if things are right. This change in approach makes testing better and changes how we do it.
Black Box Testing
Black box testing in the context of Android development is like examining an app without knowing its internal workings. It’s as if you’re looking at a black box that does something, but you don’t know how it’s doing it.
When you test an Android app using black box testing, you’re mainly interested in how the app behaves on the outside — how it responds to different inputs, buttons, and actions. You’re not concerned with the code and technical details inside the app. It’s like testing a TV remote control without knowing its circuitry.
This type of testing helps you ensure that the app works correctly and delivers the expected results to users, regardless of the intricacies of the programming code. It’s a way to catch problems that users might encounter without having to dive into the complex coding stuff.
The Power of Truth
In the JUnit framework, assertions are key to effective testing. They validate expected behavior against actual results, ensuring that app components work as intended. Truth
, a library developed by Google, builds upon this concept with expressive and readable assertions. In the context of black box testing, Truth
plays a pivotal role in verifying an app's external functionality. It not only enhances test case clarity but also helps pinpoint the root causes of failures, simplifying the debugging process.
Advantages of Using Truth in Black Box Testing
- Clarity and Expressiveness:
Truth
offers natural language assertions that read like plain English sentences, capturing expected outcomes with clarity. This enhances the readability of test cases, making them more understandable for developers. - Immediate Feedback: Similar to traditional assertions, when a
Truth
condition isn't met, the test fails instantly. This prompt feedback accelerates the debugging process, enabling fast identification of the issue's source. - Enhanced Maintenance: As apps evolve, tests must adapt.
Truth
provides clear references for expected outcomes, simplifying test updates after code changes. - Diagnostic Information:
Truth
provides rich diagnostic messages, aiding developers in understanding assertion failures by displaying meaningful context.
Black Box Testing and Verify
Black box testing, as an approach, aligns naturally with the concept of verify
often used in testing frameworks like Mockito
. The synergy between Truth
and verify
is evident when testing interactions between different components.
However, it’s important to note that while verify
is powerful for testing interactions, it's not suited for black box testing where the internal implementation details of interactions are not the focus. In such scenarios, focusing on the final outcomes using Truth
assertions is more appropriate.
Usage of verify aligns more with the concept of White box testing. It is like checking the insides of a functionality. Tests look at the code,logic and interactions to make sure everything works right. It’s different from black box testing where they only check how the machine behaves from the outside.
Where Not to Use verify in Black Box Testing
While verify
is effective for checking method invocations and interactions between components, it's not suitable for:
- Black Box Testing: In black box testing, the focus is on the external behavior of the app, not on the specifics of interactions between components.
- Final Outcomes:
Verify
is more about checking intermediate interactions. For final outcomes, useTruth
assertions that directly focus on what the app should produce.
Code Examples Illustrating Truth in Black Box Testing
Let’s explore practical code examples:
Basic Truth Example:
import com.google.common.truth.Truth.assertThat
import org.junit.Test
class CalculatorTest {
@Test
fun testAddition() {
val result = Calculator.add(2, 3)
assertThat(result).isEqualTo(5)
}
}
In this example, we test a Calculator
class for addition. The isEqualTo
method from the Truth
library verifies that the result of the addition matches the expected value.
Using Truth for Collection Assertions:
import com.google.common.truth.Truth.assertThat
import org.junit.Test
class ListManipulatorTest {
@Test
fun testListManipulation() {
val list = listOf("apple", "banana", "cherry")
assertThat(list).containsAtLeast("apple", "banana")
}
}
In this example, we’re using Truth
to test a ListManipulator
class that manipulates lists. The containsAtLeast
assertion ensures that the list contains at least the specified elements.
Using Truth for Descriptive Assertions:
import com.google.common.truth.Truth.assertThat
import org.junit.Test
class TemperatureConverterTest {
@Test
fun testCelsiusToFahrenheitConversion() {
val celsiusValue = 20.0
val fahrenheitValue = TemperatureConverter.celsiusToFahrenheit(celsiusValue)
assertThat(fahrenheitValue).isIn(67.0..69.0)
}
}
Here, we’re testing a TemperatureConverter
class converting Celsius to Fahrenheit using Truth
. The isIn
assertion verifies that the converted temperature falls within a specific range.
Conclusion
Black box testing with JUnit empowers Android developers to ensure functionality and performance across diverse devices. The Truth
library, a powerful alternative to traditional assertions, offers natural language assertions for expected behaviors. This enhances clarity, immediacy, and maintainability of tests, promoting transparency and robust testing practices. By embracing Truth
in black box testing, developers create tests that are not only effective but also comprehensible, ultimately contributing to the creation of high-quality Android apps. The combination of Truth
with verify
in black box testing provides a dynamic duo that ensures reliability by testing interactions between components in a clear and expressive manner. Remember, in black box testing scenarios, use Truth
to focus on final outcomes and expected behaviors, while verify
remains useful for testing component interactions in other contexts.