javaspringspring-hateoashateoas

How to replace Link.REL_SELF when migrating from spring-hateoas 1.0 to 2.4?”


I have this Junit test with is using hateoas 1.0:

@Test
public void processUserExceptionThrown() {
  when(userService.processAccount(request, account))
    .thenThrow(new RuntimeException());
  try {
    userService.processAccount(request);
    fail("Exception was expected to be thrown, but was not");
  } catch (InternalException ex) {
    assertEquals(Link.REL_SELF, ex.getSelfLink().getRel());
  }
}

I tried to migrate the code to latest Spring and hateoas:

@Test
public void processUserExceptionThrown() {
  when(userService.processAccount(request, account))
    .thenThrow(new RuntimeException());
  try {
    userService.processAccount(request);
    fail("Exception was expected to be thrown, but was not");
  } catch (InternalException ex) {
    assertEquals(Link.of("url", IanaLinkRelations.SELF), ex.getSelfLink().getRel());
  }
}

The new assertion

assertEquals(Link.of("url", IanaLinkRelations.SELF), ex.getSelfLink().getRel());

fails with

org.opentest4j.AssertionFailedError: expected: <<url>;rel="self"> but was: <self>

How can I migrate the code properly without changing the end result?


Solution

  • In your old code, you have asserted that a relation (Link.REL_SELF) is equal to a relation (ex.getSelfLink().getRel())

    In your new code, you assert that a link (Link.of("url", IanaLinkRelations.SELF)) is equal to a relation (ex.getSelfLink().getRel()).

    Change your new code so expected and actual values are of the same type - both are relations (LinkRelation).

    assertEquals(IanaLinkRelations.SELF, ex.getSelfLink().getRel());