0
assertThat(service.load(1, setup.getEntity(), "OBJECT1").getImages().get(0).isSwitchedOn()).isEqualTo(false);

I'm accesing the unique element of the list in this way,but i'm wondering if there's a more appropriate way to do it.

  • The main idea of AssertJ is that you may to write/generate own assertions (via extending AbstractObjectAssert) like instead of `assetThat(image.isSwitchedOn()).isEqualTo(false)` that should be something like `assetThat(image).isSwitchedOn()`. Start with https://joel-costigliola.github.io/assertj/assertj-assertions-generator.html – Andrey B. Panfilov Oct 10 '22 at 12:58

3 Answers3

3

There is a method called singleElement() that can assert if a collection contain only one element.

There is also another method called extracting() which allow you to extract a property from an object for assertion.

Combing them will give you :

assertThat(list).singleElement().extracting(Image::isSwitchOn).isEqualTo(false);

Notice that extracting() will return you a generic object type for further chaining the assertion . If you want it to return a more specified boolean type , you can consider to use :

assertThat(list).singleElement().extracting(Image::isSwitchOn, as(BOOLEAN)).isFalse();

Or

assertThat(list).singleElement().extracting(Image::isSwitchOn).asInstanceOf(BOOLEAN).isFalse();

BOOLEAN is the static import from the class InstanceOfAssertFactories

Ken Chan
  • 84,777
  • 26
  • 143
  • 172
0

I think , service.load(1, setup.getEntity(), "OBJECT1") returns list of images.

If you want to check the first element in the list of images for isSwitchedOn, the asset that statement is fine.

In case if you want to assert all the images -> isSwitchedOn, better use loop and use assertThat.

RagaSGNur
  • 199
  • 1
  • 2
  • 15
-1

how about use public func? make it as a static func and put it in your tool-class.But if you just have one element,why not other structs?

wxr
  • 1
  • 2
  • Beside the fact that your approach does not follow the guidelines of clean coding, it is always much more pleasing to read the text which is grammatically correct. – Draško Kokić Oct 15 '22 at 09:56