Replies: 5 comments 7 replies
-
https://stackoverflow.com/a/69211323/9714611 assertThatExceptionOfType(DataIntegrityViolationException.class).isThrownBy( () -> xrepo.save(abc) )
.havingCause()
.asInstanceOf(type(ConstraintViolationException.class))
.returns("XXXX", from(ConstraintViolationException::getConstraintName)); Type Narrowing
ReadabilityReadability could be improved. Considering the proposal above, it would read as:
The I see two options:
|
Beta Was this translation helpful? Give feedback.
-
From #2616 (comment): Currently assertThat(entity)
.usingRecursiveComparison()
.withEqualsForFields((String s, PhoneType t) -> PhoneType.valueOf(s) == t, "phones.phoneType")
.withEqualsForFields((String s, AddressType t) -> AddressType.valueOf(s) == t, "addresses.addressType")
// etc..
.isEqualTo(dto); It would be nice if this was supported OOTB via an option, e.g.: assertThat(entity)
.usingRecursiveComparison()
.withEnumStringComparison()
.isEqualTo(dto); |
Beta Was this translation helpful? Give feedback.
-
Is there an option to supporting working with Kotlin Coroutines in a slightly cleaner way? My thought would be that this could be done as an extension module for assertj. Perhaps adding This would primarily improve the usage of code block based assertions. At the moment working with this library I can fairly cleanly write a test that is using an fun testMeaningOfLife() {
val result = runBlocking {
meaningOfLife()
}
assertThat(result).isEqualTo(42)
} The fun testMeaningOfLife(): Unit = runBlocking {
val result = meaningOfLife()
assertThat(result).isEqualTo(42)
} Where this could be tweaked is with the code based assertions ( import org.assertj.kotlin.api.Assertions.isThrownBy
fun testMeaningOfLife() {
assertThatNoException().isThrownBy {
// No runBlocking call required here as the callable on isThrownBy is marked as a `suspend`-ing method
meaningOfLife()
}
} The change here is that the fun NotThrownAssert.isThrownBy(callable: suspend () -> Unit) {
val throwable = catchSuspendedThrowable(callable)
assertThat(throwable).`as`(description).doesNotThrowAnyException()
}
fun catchSuspendedThrowable(callable: suspend () -> Unit): Throwable? {
try {
runBlocking {
callable.call()
}
} catch (t: Throwable) {
return t
}
} The challenge here is that things like |
Beta Was this translation helpful? Give feedback.
-
I kind of find some assertions can be a bit jarring due to how they are worded. One of the main ones I see is assertThatCode. I've tried to suggest to my team to prefer assertThatNoException or assertThatExceptionOfType is preferred for a couple of reasons... assertThatCode(this::createPotatoExceptionFactory)
.isInstanceOf(PotatoException.class); In this case, it is hard to tell that this is testing an exception is thrown rather than something being returned. In addition, the methods that consume lambdas as the first call can make messy code very easy compared to those that have a chained call first. Some auto formatters make these very hard to read. An example of how a formatter might make these awkward when using static imports: assertThatThrownBy(() -> potatoFactory
.newInstance().createPotato())
.isInstanceOf(PotatoException.class); ...where it is hard to see that there is a lambda and where it ends without reading the whole thing first. Given there is assertThatNoException and assertThatExceptionOfType, and those work much better for this, I do wonder if the former methods could be deprecated going forwards in an attempt to enforce "one good way to do things" and keep the assertions as close to fluent language as possible without compromising code readability. AssertJ does provide a lot of ways of doing certain things. While that is usually very helpful, it can make enforcing consistent tests in a team somewhat difficult because of stuff like this. |
Beta Was this translation helpful? Give feedback.
-
Map assertion may get as expected list of entries or other map . |
Beta Was this translation helpful? Give feedback.
-
Are you happy with the fluency of your assertions? Do you have a use case you wish would be less verbose?
Put your use case here 👇 and let's see what we can do!
Beta Was this translation helpful? Give feedback.
All reactions