2019-03-10 18:19:46 +01:00
|
|
|
# Cajon - Concise AssertJ Optimizing Nitpicker
|
|
|
|
|
2019-03-31 15:15:06 +02:00
|
|
|
Cajon is an IntelliJ IDEA Plugin for shortening and optimizing AssertJ assertions.
|
2019-03-10 18:19:46 +01:00
|
|
|
|
|
|
|
## Why?
|
|
|
|
|
2019-03-24 09:15:07 +01:00
|
|
|
First, code is easier to read, when it is concise and reflects the intention clearly.
|
|
|
|
AssertJ has plenty of different convenience methods that describing various intentions precisely.
|
2019-03-10 18:19:46 +01:00
|
|
|
Why write longer, more complex code that can be expressed in brevity?
|
|
|
|
|
|
|
|
Second, AssertJ is able to output more meaningful descriptions when an assertion fails.
|
2019-03-24 09:15:07 +01:00
|
|
|
This makes finding bugs and fixing failed tests more efficient.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
assertThat(collection.size()).isEqualTo(5);
|
|
|
|
```
|
2019-03-24 09:15:07 +01:00
|
|
|
|
|
|
|
If the collection has more or less than 5 elements, the assertion will fail, but will not
|
|
|
|
tell you about the contents, making it hard to guess what went wrong.
|
|
|
|
|
|
|
|
Instead, if you wrote the same assertion the following way:
|
|
|
|
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
assertThat(collection).hasSize(5);
|
|
|
|
```
|
2019-03-24 09:15:07 +01:00
|
|
|
|
|
|
|
Then AssertJ would tell you the contents of the collection on failure.
|
2019-03-10 18:19:46 +01:00
|
|
|
|
2019-03-31 15:15:06 +02:00
|
|
|
## Conversion of JUnit assertions to AssertJ
|
|
|
|
|
|
|
|
The plugin also supports the conversion of the most common JUnit 4 assertions to AssertJ.
|
|
|
|
|
2019-03-10 18:19:46 +01:00
|
|
|
## Implemented
|
|
|
|
|
|
|
|
- AssertThatObjectIsNull
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
from: assertThat(object).isEqualTo(null);
|
|
|
|
to: assertThat(object).isNull();
|
|
|
|
```
|
2019-03-10 18:19:46 +01:00
|
|
|
- AssertThatObjectIsNotNull
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
from: assertThat(object).isNotEqualTo(null);
|
|
|
|
to: assertThat(object).isNotNull();
|
|
|
|
```
|
2019-03-10 18:19:46 +01:00
|
|
|
- AssertThatBooleanIsTrueOrFalse
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
from: assertThat(booleanValue).isEqualTo(true/false/Boolean.TRUE/Boolean.FALSE);
|
|
|
|
to: assertThat(booleanValue).isTrue()/isFalse();
|
|
|
|
```
|
2019-03-23 22:44:22 +01:00
|
|
|
- AssertThatStringIsEmpty
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
from: assertThat(charSequence/string).isEqualTo("");
|
|
|
|
from: assertThat(charSequence/string).hasSize(0);
|
|
|
|
to: assertThat(charSequence/string).isEmpty();
|
|
|
|
```
|
2019-03-24 14:42:02 +01:00
|
|
|
- AssertThatEnumerableIsEmpty
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
from: assertThat(enumerable).hasSize(0);
|
|
|
|
to: assertThat(enumerable).isEmpty();
|
|
|
|
- JUnitAssertToAssertJ
|
|
|
|
```
|
|
|
|
assertTrue(condition);
|
|
|
|
assertTrue(message, condition);
|
|
|
|
assertFalse(condition);
|
|
|
|
assertFalse(message, condition);
|
|
|
|
assertNull(object);
|
|
|
|
assertNull(message, object);
|
|
|
|
assertNonNull(object);
|
|
|
|
assertNonNull(message, object);
|
|
|
|
assertEquals(expected, actual);
|
|
|
|
assertEquals(message, expected, actual);
|
|
|
|
assertEquals(expectedDoubleOrFloat, actualDoubleOrFloat, delta);
|
|
|
|
assertEquals(message, expectedDoubleOrFloat, actualDoubleOrFloat, delta);
|
|
|
|
assertNotEquals(unexpected, actual);
|
|
|
|
assertNotEquals(message, unexpected, actual);
|
|
|
|
assertNotEquals(unexpectedDoubleOrFloat, actualDoubleOrFloat, delta);
|
|
|
|
assertNotEquals(message, unexpectedDoubleOrFloat, actualDoubleOrFloat, delta);
|
|
|
|
assertSame(expected, actual);
|
|
|
|
assertSame(message, expected, actual);
|
|
|
|
assertNotSame(unexpected, actual);
|
|
|
|
assertNotSame(message, unexpected, actual);
|
|
|
|
assertArrayEquals(expected, actual);
|
|
|
|
assertArrayEquals(message, expectedArray, actualArray);
|
|
|
|
assertArrayEquals(expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
|
|
|
|
assertArrayEquals(message, expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
|
|
|
|
```
|
2019-03-24 09:15:07 +01:00
|
|
|
|
|
|
|
## TODO
|
2019-03-10 18:19:46 +01:00
|
|
|
- AssertThatArrayHasLiteralSize
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
from: assertThat(array.length).isEqualTo(literal); literal > 0
|
|
|
|
to: assertThat(array).hasSize(literal);
|
|
|
|
```
|
2019-03-10 18:19:46 +01:00
|
|
|
- AssertThatArrayHasEqualSize
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
from: assertThat(array.length).isEqualTo(anotherArray.length);
|
|
|
|
to: assertThat(array).hasSameSizeAs(anotherArray);
|
|
|
|
from: assertThat(array.length).isEqualTo(iterable.size());
|
|
|
|
to: assertThat(array).hasSameSizeAs(iterable);
|
|
|
|
```
|
2019-03-10 18:19:46 +01:00
|
|
|
- AssertThatArrayIsEmpty
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
from: assertThat(array.length).isEqualTo(0);
|
|
|
|
from: assertThat(array.length).isLessThanOrEqualTo(0);
|
|
|
|
from: assertThat(array.length).isLessThan(1);
|
|
|
|
from: assertThat(array).hasSize(0);
|
|
|
|
to: assertThat(array).isEmpty();
|
|
|
|
```
|
2019-03-10 18:19:46 +01:00
|
|
|
- AssertThatArrayIsNotEmpty
|
2019-03-31 15:15:06 +02:00
|
|
|
```
|
|
|
|
from: assertThat(array.length).isGreaterThan(0);
|
|
|
|
to: assertThat(array).isNotEmpty();
|
|
|
|
```
|
|
|
|
- AssertThatCollectionHasLiteralSize
|
|
|
|
```
|
|
|
|
from: assertThat(collection.size()).isEqualTo(literal); literal > 0
|
|
|
|
to: assertThat(collection).hasSize(literal);
|
|
|
|
```
|
|
|
|
- AssertThatCollectionHasEqualSize
|
|
|
|
```
|
|
|
|
from: assertThat(collection.size()).isEqualTo(anotherArray.length);
|
|
|
|
to: assertThat(collection).hasSameSizeAs(anotherArray);
|
|
|
|
from: assertThat(collection.size()).isEqualTo(anotherCollection.size());
|
|
|
|
to: assertThat(collection).hasSameSizeAs(anotherCollection);
|
|
|
|
```
|
|
|
|
- AssertThatCollectionIsNotEmpty
|
|
|
|
```
|
|
|
|
from: assertThat(collection.size()).isGreaterThan(0);
|
|
|
|
from: assertThat(collection.size()).isGreaterThanOrEqualTo(1);
|
|
|
|
to: assertThat(collection).isNotEmpty();
|
|
|
|
```
|
|
|
|
- AssertThatCollectionIsEmpty
|
|
|
|
```
|
|
|
|
from: assertThat(collection.size()).isEqualTo(0);
|
|
|
|
from: assertThat(collection.size()).isLessThanOrEqualTo(0);
|
|
|
|
from: assertThat(collection.size()).isLessThan(1);
|
|
|
|
from: assertThat(collection).hasSize(0);
|
|
|
|
to: assertThat(collection).isEmpty();
|
|
|
|
```
|
2019-03-24 09:15:07 +01:00
|
|
|
- AssertThatGuavaOptionalContains
|