Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7060891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:24:27+00:00 2026-05-28T04:24:27+00:00

I have UI automation tests. Tests involve three entities – Data object class –

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:24:27+00:00Added an answer on May 28, 2026 at 4:24 am

    Consider using ThreadLocal. This way each thread has its own copy of HelperClass. Note that synchronizing separate methods won’t give you anything – changes made in one test (in one thread) are visible by other tests

    class ParallelDataObject {
    
        private final ThreadLocal<HelperClass> helperClassThreadLocal = new ThreadLocal<HelperClass>() {
    
            @Override
            protected HelperClass initialValue() {
                return new HelperClass(new Data());
            }
        };
    
        private HelperClass helperClass() {
            return helperClassThreadLocal.get();
        }
    
        @Test
        public void passM1() {
            helperClass().verifyFlag();
        }
    
        @Test
        public void failM2() {
            helperClass().getData().setFlag(false);
            helperClass().verifyFlag();
        }
    
    }
    
    class HelperClass {
    
        private final Data data;
    
        public HelperClass(Data data) {
            this.data = data;
        }
    
        public Data getData() {
            return data;
        }
    
        public void verifyFlag() {
            assert data.getFlag();
        }
    }
    
    class Data {
        private boolean flag = true;
    
        public Data setFlag(boolean flag) {
            this.flag = flag;
            return this;
        }
    
        public boolean getFlag() {
            return flag;
        }
    }
    

    Other improvements:

    • passM3 and failM4 were superfluous
    • since HelperClass requires an instance of Data to work, it should declare it using constructor dependency
    • when using:

      synchronized(this)
      

      wrapping whole method body, consider using synchronized keyword in method declaration instead (more readable).

    • synchronization is no longer needed with ThreadLocals

    Test statelessness

    @gpeche makes a good suggestion that tests should be independent. Unfortunately (why, oh why!?) JUnit reuses the same test case class instance (ParallelDataObject in 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):

    class ParallelDataObject {
    
        @Test
        public void passM1() {
            final HelperClass helperClass = new HelperClass(new Data());
    
            helperClass.verifyFlag();
        }
    
        @Test
        public void failM2() {
            final Data data = new Data();
            data.setFlag(false);
    
            final HelperClass helperClass = new HelperClass(data);
    
            helperClass.verifyFlag();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While writing UI automation tests I come across forms with many fields and instead
I have recently completed a detailed investigation regarding GP functional test automation possibilities with
We use Perl for GUI test automation. It has been very successful. We have
I have a few Silverlight UI tests that I'm automating with White. These tests
I have used automation to insert values into a cell, however I have never
I have the following automation code: lPrintSetup := fWordObject.Application.Dialogs.Item(wdDialogFilePrintSetup); lPrintSetup.Printer := 'MyPrinter'; lPrintSetup.DoNotSetAsSysDefault :=
Been working on a personal add-in for VS 2008 and have been researching automation
I have an application that uses MSWord automation to edit some documents, after they
We have an application (a custom network management tool for building automation) that supports
I have a Windows Workflow application that uses classes I've written for COM automation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.