Gaffer icon indicating copy to clipboard operation
Gaffer copied to clipboard

Improve assertions and use assertJ

Open t92549 opened this issue 2 years ago • 3 comments

In many places in Gaffer, JUnit assertions are used and are quite ugly compared to the assertJ equivalent. See the following example for testing lists: JUnit: https://github.com/gchq/Gaffer/blob/6a0c8636f71c47096fc3d99401688b8815a32933/core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/get/GetElementsTest.java#L144 assertJ: https://github.com/gchq/Gaffer/blob/6a0c8636f71c47096fc3d99401688b8815a32933/core/data/src/test/java/uk/gov/gchq/gaffer/data/graph/function/walk/ExtractWalkEdgesFromHopTest.java#L55

There are also many other occasions that assertJ can provide easier to read and stronger testing, like when using custom Conditions: https://github.com/gchq/Gaffer/blob/370a7fa6e5fe99a9e5f06fa2a5e5b3d53db94339/store-implementation/accumulo-store/src/test/java/uk/gov/gchq/gaffer/accumulostore/integration/AccumuloMatchedVertexIT.java#L102

There are also places where assertJ assertions are used but could be cleaned up and easier to read, such as directly casting where .asList() could be used: https://github.com/gchq/Gaffer/blob/370a7fa6e5fe99a9e5f06fa2a5e5b3d53db94339/core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/resultcache/handler/GetGafferResultCacheExportHandlerTest.java#L133

These usages should be cleaned up, producing less warnings in tests, and making them easier to read.

t92549 avatar Apr 07 '22 11:04 t92549

To help with completing this ticket I've had a look through the codebase for where these improvements can be made.

Simplification of assertEquals(Lists.newArrayList()). Using grep -rFIl "assertEquals(Lists.newArrayList" . finds:

core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToSetTest.java
core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToStreamTest.java
core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToMapTest.java
core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/get/GetAdjacentIdsTest.java
core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/add/AddElementsTest.java
rest-api/common-rest/src/test/java/uk/gov/gchq/gaffer/rest/factory/AbstractExamplesFactoryTest.java

Simplification of object is of expected class. Using grep -rEIl "assertEquals\(\w*\.class," . finds:

./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/ValidateOperationChainTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/CountGroupsTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToCsvTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToListTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToSetTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToVerticesTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToStreamTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToEntitySeedsTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToArrayTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/output/ToMapTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/get/GetAdjacentIdsTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/function/FilterTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/LimitTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/GetWalksTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/compare/MinTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/compare/MaxTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/compare/SortTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/generate/GenerateObjectsTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/generate/GenerateElementsTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/ValidateTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/ScoreOperationChainTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/CountTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/job/GetJobDetailsTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/export/GetExportsTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/export/set/GetSetExportTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/export/set/ExportToSetTest.java
./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/export/resultcache/ExportToGafferResultCacheTest.java
./core/graph/src/test/java/uk/gov/gchq/gaffer/operation/export/graph/handler/ExportToOtherGraphHandlerTest.java
./core/graph/src/test/java/uk/gov/gchq/gaffer/graph/GraphTest.java
./core/type/src/test/java/uk/gov/gchq/gaffer/serialisation/FreqMapSerialiserTest.java
./core/type/src/test/java/uk/gov/gchq/gaffer/serialisation/AvroSerialiserTest.java
./core/data/src/test/java/uk/gov/gchq/gaffer/data/elementdefinition/view/ViewTest.java
./core/data/src/test/java/uk/gov/gchq/gaffer/data/elementdefinition/view/NamedViewTest.java
./core/data/src/test/java/uk/gov/gchq/gaffer/data/elementdefinition/view/ViewUtilTest.java
./core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/handler/GetFromEndpointHandlerTest.java
./core/store/src/test/java/uk/gov/gchq/gaffer/store/operation/OperationUtilTest.java
./core/store/src/test/java/uk/gov/gchq/gaffer/store/operations/OperationDeclarationsTest.java
./core/store/src/test/java/uk/gov/gchq/gaffer/store/SerialisationFactoryTest.java
./core/store/src/test/java/uk/gov/gchq/gaffer/store/schema/SchemaTest.java
./core/store/src/test/java/uk/gov/gchq/gaffer/store/schema/SchemaElementDefinitionTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/jsonSerialisation/JSONSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/MapSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/JavaSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/StringSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/raw/CompactRawLongSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/raw/CompactRawIntegerSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/ordered/OrderedLongSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/ordered/OrderedIntegerSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/ordered/OrderedFloatSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/ordered/OrderedDateSerialiserTest.java
./core/serialisation/src/test/java/uk/gov/gchq/gaffer/serialisation/implementation/ordered/OrderedDoubleSerialiserTest.java
./example/road-traffic/road-traffic-demo/src/test/java/uk/gov/gchq/gaffer/graph/GraphConfigTest.java
./library/bitmap-library/src/test/java/uk/gov/gchq/gaffer/bitmap/serialisation/RoaringBitmapSerialiserTest.java
./library/bitmap-library/src/test/java/uk/gov/gchq/gaffer/bitmap/serialisation/json/RoaringBitmapJsonSerialisationTest.java
./library/spark/spark-library/src/test/java/uk/gov/gchq/gaffer/spark/serialisation/kryo/RegistratorTest.java
./rest-api/spring-rest/src/test/java/uk/gov/gchq/gaffer/rest/config/FactoryConfigTest.java
./rest-api/common-rest/src/test/java/uk/gov/gchq/gaffer/rest/factory/GraphFactoryTest.java
./rest-api/common-rest/src/test/java/uk/gov/gchq/gaffer/rest/factory/UserFactoryTest.java
./rest-api/common-rest/src/test/java/uk/gov/gchq/gaffer/rest/factory/AbstractExamplesFactoryTest.java
./rest-api/common-rest/src/test/java/uk/gov/gchq/gaffer/rest/serialisation/ObjectMapperProviderTest.java
./store-implementation/accumulo-store/src/test/java/uk/gov/gchq/gaffer/accumulostore/AccumuloSerialisationFactoryTest.java
./store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/handler/FederatedOperationHandlerTest.java
./store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphWithHooksTest.java
./store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/operation/AddGraphTest.java

Simplification of AssertJ isEqualTo(Lists.newArrayList. Using grep -rFIl ".isEqualTo(Lists.newArrayList(" . finds:

./core/operation/src/test/java/uk/gov/gchq/gaffer/operation/impl/get/GetElementsTest.java
./core/common-util/src/test/java/uk/gov/gchq/gaffer/commonutil/iterable/CachingIterableTest.java
./integration-test/src/test/java/uk/gov/gchq/gaffer/integration/impl/IfIT.java
./store-implementation/federated-store/src/test/java/uk/gov/gchq/gaffer/federatedstore/FederatedStoreInputChainTest.java

Ideally all use of AssertEquals could be removed. In most cases an AssertJ assertion will be simpler to read. An example would be replacing assertEquals(expected.size(), actual.size()) with assertThat(actual).hasSize(expected) using AssertJ hasSize().

Running grep -rFIl "import static org.junit.jupiter.api.Assertions.assertEquals;" . reveals 418 files which have some use of AssertEquals.

GCHQDeveloper314 avatar Aug 22 '23 08:08 GCHQDeveloper314

Reopening as not fully completed by #3014

t92549 avatar Nov 13 '23 17:11 t92549