SRCM
SRCM

Reputation: 271

Eclipse Code Refactoring - Junits

I want to refactor exisiting code

String a = "Hi";
assertEquals(a, "Hi");

to

String a = "Hi";
assertEquals("Hi", a);

I have like 2 million assert statements all over my project that need to be refactored. Are they any shortcuts keys, templates or batch process in Eclipse IDE which I can implement to do this more easily?

assertEquals(expected, actual);

Upvotes: 2

Views: 179

Answers (2)

Arthur Eirich
Arthur Eirich

Reputation: 3638

Having played a bit with the regex, I came up with the following solution (thanks to this link):

assertEquals\((.*), (.*)\);

captures the first and the second argument into a separate group 1 and group 2 respectively and should be pasted into the find input field.

assertEquals($2, $1);

This command will swap the groups which were stored in $1 for the first and $2 for the second argument respectively.

WARNING

I am using Intellij IDEA for developing, thus it can come to an unexpected behaviour in eclipse. But the command should be almost the same ones.

Upvotes: 1

Gediminas Rimsa
Gediminas Rimsa

Reputation: 646

You could try the following:

  1. Copy org.junit.Assert.assertEquals methods into a new class, let's say temp.TempAssert
  2. Search and replace import static org.junit.Assert.assertEquals; with import static temp.TempAssert.assertEquals; in all files
  3. Go to TempAssert class and do Refactor -> Change Method Signature to switch order of parameters
  4. Replace import static temp.TempAssert.assertEquals; back to import static org.junit.Assert.assertEquals; in all files

Upvotes: 2

Related Questions