How to Use Java Multithreading for Database Operations in Android UIAutomator Tests
The article demonstrates a simple Java ThreadTest class that enables concurrent database updates during Android UIAutomator and Selenium‑Java test cases, showing how to control execution with key flags, integrate the thread into test methods, and optionally use join() to avoid exceptions.
When writing automated UI tests for Android with UIAutomator or for web applications with Selenium‑Java, it is common to need database verification or cleanup after each test case. Running a separate SQL statement for every test quickly defeats the purpose of automation, especially when many test cases are involved.
To address this, the author created a lightweight ThreadTest class that runs in its own thread and performs a database update based on a controllable flag. The implementation is deliberately simple: a boolean key keeps the thread alive, while a second flag key1 determines whether the database operation should be executed.
public class ThreadTest extends Thread {
boolean key = true;
boolean key1 = false;
public void run() {
while (key) {
if (key1) {
try {
MySql.UpdateLectureById(123456);
key = false;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
}
public void setKey1(boolean key) {
this.key1 = key;
}
public void setKey(boolean key) {
this.key = key;
}
}In a test method, the thread is instantiated, the key1 flag is set to false (or true when the database action is required), and the test proceeds with the usual UI steps. An example using Selenium‑Java (or UIAutomator) shows how the thread integrates with the test flow:
public void testLearnCornerAddQuestion() throws UiObjectNotFoundException, InterruptedException, IOException {
begin();
login();
waitForTextAndClick("聊天");
waitForTextAndClick("学习角");
postNewCard(); // post a new question
ThreadTest one = new ThreadTest();
one.setKey1(false); // control whether DB update runs
over();
}The main method demonstrates how to start the thread and, for UIAutomator, how to launch a helper that generates a test report. The author notes that, in real usage, a join() call may be added after starting the thread to ensure the main test thread waits for the background operation to finish, thereby preventing unexpected exceptions.
public static void main(String[] args) {
new ThreadTest().start();
new UiAutomatorHelper("Demo", "student.Test", "testTest", "1");
new ThreadTest().setKey(false);
// Optionally: thread.join();
}This approach lets developers keep test cases concise while still performing necessary database checks or clean‑ups in parallel, preserving the efficiency goals of automated testing.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
