I have UI automation tests. Tests involve three entities –
Data object class – data to be filled in forms. Herein each form on a page could be represented by a different data object.
Helper class – which fills in data in a form on page
Test class – which uses data object and helper class to perform test.
Following is the cut down version of test –
public class ParallelDataObject {
HelperClass helperClass = new HelperClass();
Data data;
@BeforeMethod
public void setTestData() {
data = new Data();
helperClass.setData(data);
}
@Test
public void passM1() {
helperClass.verifyFlag();
}
@Test
public void failM2() {
data.setFlag(false);
helperClass.setData(data);
helperClass.verifyFlag();
}
@Test
public void passM3() {
helperClass.verifyFlag();
}
@Test
public void failM4() {
data.setFlag(false);
helperClass.setData(data);
helperClass.verifyFlag();
}
}
class HelperClass {
Data data;
public void setData(Data data) {
synchronized (data) {
this.data = data;
}
}
public void verifyFlag() {
synchronized (data) {
assert data.getFlag();
}
}
}
class Data {
private boolean flag;
public Data() {
flag = true;
}
public Data setFlag(boolean flag) {
synchronized (this) {
this.flag = flag;
return this;
}
}
public boolean getFlag() {
synchronized (this) {
return flag;
}
}
When executing methods in parallel I encountered weird results as data is not thread safe. Then I incorporated synchronize blocks but yet I encounter weird results.
I am sure I have messed up how synchronization should be used here in. Any insight?
I did one more exercise. I set up another Test class exactly same as first test class. I removed all synchronization from helper and data class. When I run classes in parallel (instead of methods). Test results are as expected. Why don’t I run in to concurrency when I execute classes in parallel, even though they user same helper class and data object?
Consider using
ThreadLocal. This way each thread has its own copy ofHelperClass. Note that synchronizing separate methods won’t give you anything – changes made in one test (in one thread) are visible by other testsOther improvements:
passM3andfailM4were superfluousHelperClassrequires an instance ofDatato work, it should declare it using constructor dependencywhen using:
wrapping whole method body, consider using
synchronizedkeyword in method declaration instead (more readable).synchronization is no longer needed with
ThreadLocalsTest statelessness
@gpeche makes a good suggestion that tests should be independent. Unfortunately (why, oh why!?) JUnit reuses the same test case class instance (
ParallelDataObjectin this case) for all test methods execution. This means that assigning any stateful objects to test case class fields is dangerous and must be avoided.In this particular case the OP would have to create a new instance of HelperClass in each test method (which, in fact, isn’t such a bad idea):