Assertj-core: Convenience method .asType as shorthand for .asInstanceOf(InstanceOfAssertFactories.type(...))

Created on 22 May 2021  路  8Comments  路  Source: assertj/assertj-core

Feature request :-)

Summary

The .asInstanceOf method is a great recent addition. It would be nice to make the syntax more concise for the most common use case of casting to a class. This is particularly relevant when dealing with exceptions

Example

@Test
void checkExceptionCode() {
        assertThatThrownBy(() -> dbOperation())
                .asType(SQLException.class)
                .extracting(SQLException::getErrorCode)
                .isEqualTo(code);
}

@Test
void checkExceptionCode() {
        assertThatThrownBy(() -> dbOperation())
                .extractingFromType(SQLException.class, SQLException::getErrorCode)
                .isEqualTo(code);
}

// Implementation

  public <T> ObjectAssert<T> asType(Class<T> type) {
    return asInstanceOf(InstanceOfAssertFactories.type(type));
  }

Most helpful comment

@jhannes the following is probably what you are looking for:

assertThatExceptionOfType(SQLException.class).isThrownBy(() -> dbOperation())
  .extracting(SQLException::getErrorCode)
  .isEqualTo(code);

All 8 comments

Something similar has been discussed in #1547 and we decided not to introduce it because it could give a false impression that e.g. asType(String.class) could return string assertions, but this would not be the case.

After almost two years, I tend to have the same opinion. @assertj/core what do you think about it?

@scordio I was actually initially surprised that asInstanceOf(InstanceOfAssertFactories.type(String.class)) didn't return a AbstractStringAssert, until I remembered that I was working in Java 馃槀

My example from assertThatThrownBy is a pretty common use case for me, however, as I have exceptions returning enum values to be returned by my REST API. A less clunky alternative to asInstanceOf(InstanceOfAssertFactories.type(MyException.class)).extracting(MyException::getErrorCode) would be appreciated

Something similar has been discussed in #1547 and we decided not to introduce it because it could give a false impression that e.g. asType(String.class) could return string assertions, but this would not be the case.
After almost two years, I tend to have the same opinion. @assertj/core what do you think about it?

I still agree with that decision, we also have asString() that return String assertions.

@jhannes here's an alternative:

assertThatThrownBy(() -> dbOperation())
          .extracting("errorCode")
          .isEqualTo(code);

It's not as refactoring friendly as a method reference but it does the job and the syntax is concise.

I think your suggestion extractingFromType(SQLException.class, SQLException::getErrorCode) is probably the most concise option for a strongly typed version of extracting.

I really prefer not to use extracting(String) as I have enough costs to refactoring already.

asString is a nice addition. But the analogy with asType is not accurate, as asInstanceOf with String will (obviously) not return AbstractStringAssert either

@jhannes the following is probably what you are looking for:

assertThatExceptionOfType(SQLException.class).isThrownBy(() -> dbOperation())
  .extracting(SQLException::getErrorCode)
  .isEqualTo(code);

@scordio Yes! 鉂わ笍

The only sad thing is that I just rewrote a bunch of tests with try/catch to use assertThatThrownBy 馃槶

Sorry about that, it took me some time to see the solution from a different angle.

There is actually another issue related to this topic: #2242

Was this page helpful?
0 / 5 - 0 ratings