Eclipse: Flip arguments using regular expressions
Lately I have migrated a project from JUnit to TestNG. With the help of an Ecplipse plugin this is a one-click task. Every assert-method is replaced by its counterpart in class org.testng.AssertJUnit
. This is only an intermediate solution. The final goal is to use org.testng.Assert
. This will present a small challenge. JUnit’s two arg assert-methods, e.g. assertEquals
or assertSame
define order expected, actual. TestNG defines the opposite. Therefore argument order needs to be flipped.
Flipping argument order is achived with the help of Find/Replace and regular expressions. To find all assertEquals-methods and flip their arguments, follow 5 steps:
- Within the respective test class call the Find/Replace-dialog, either via menu entry Edit or via [Strg]-F.
- Check Regular expressions.
- Enter
assertEquals\(([^,]+),\s*([^)]+)\s*\)
into text field Find. - Enter
assertEquals(\2, \1)
into text field Replace. - Click button Replace All.
The regular expression in text field Find makes use of two capturing groups. Capturing groups are created with ([^,]+)
and ([^)]+)
. They are referenced in text field Replace in reverse order with \2
and \1
.