UIBarButtonItem does not implement UIAccessibilityIdentification , so setting accessibilityIdentifier does not work.
Try instead
userBarButtonItem.accessibilityLabel = "userBarButtonItem"
And then in the test case
XCUIApplication().buttons["userBarButtonItem"]
That should work.
UPDATE:
Now UIBarButtonItem corresponds to UIAccessibilityIdentification, so all this is not required.
source share