Converting JUnit assertions to Fest Assertions

joel-costigliola edited this page Jul 9, 2012 · 9 revisions

(to main page)

This page will help converting all your JUnit assertions to Fest Assertions.
Note that FEST & JUnit assertions can coexist together, you don't have to migrate all in one time.

Here's a list of find/replace based on regexp that allow to change JUnit assertion into FEST assertion (don't forget to check regexp mode in your editor replace window).

If you are using IntelliJ IDEA, you can use the Structured Search and Replace (SSR) feature.

The order of find/replace is important, so that you can benefit of the most relevant Fest assertions. For example you should convert assertEquals(0, myList.size()) to assertThat(myList).isEmpty() instead of assertThat(myList.size()).isEqualTo(0)

Finally, we plan to create an eclipse plugin that will ease/automate the conversion task.


1 - Converting assertEquals(0, myList.size()) to assertThat(myList).isEmpty()
Find/replace Regexp :

assertEquals(0,(.*).size()); -> assertThat(\1).isEmpty();

It's important to run this one before the assertEquals -> isEqualTo, to avoid ending with : assertThat(myList.size()).isEqualTo(0)


2 - Converting assertEquals(expectedSize, myList.size()) to assertThat(myList).hasSize(expectedSize)
Find/replace Regexp :

assertEquals((.*),(*.).size()); -> assertThat(\2).hasSize(\1);

It's important to run this one before the assertEquals -> isEqualTo, to avoid ending with : assertThat(myList.size()).isEqualTo(expectedSize)


3 - Converting assertEquals(value, valueUnderTest) to assertThat(valueUnderTest).isEqualTo(value)
Find/replace Regexp :

assertEquals((.),(.)); -> assertThat(\2).isEqualTo(\1);`

4 - Converting assertNotEquals(value, valueUnderTest) to assertThat(valueUnderTest).isNotEqualTo(value)
Find/replace Regexp :

assertNotEquals((.),(.)); -> assertThat(\2).isNotEqualTo(\1);

5 - Converting assertNull(objectUnderTest) to assertThat(objectUnderTest).isNull()
Find/replace Regexp :

assertNull((.*)); -> assertThat(\1).isNull();

6 - Converting assertNotNull(objectUnderTest) to assertThat(objectUnderTest).isNotNull()
Find/replace Regexp :

assertNotNull((.*)); -> assertThat(\1).isNotNull();

7 - Converting assertTrue(logicalCondition) to assertThat(logicalCondition).isTrue()

Find/replace Regexp :

assertTrue((.*)); -> assertThat(\1).isTrue();

8 - Converting assertFalse(logicalCondition) to assertThat(logicalCondition).isFalse()
Find/replace Regexp :

assertFalse((.*)); -> assertThat(\1).isFalse();