最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

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

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

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

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

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

Share Improve this question edited Mar 11 at 21:03 Geba 2,22311 silver badges16 bronze badges asked Mar 11 at 11:29 Peter PenzovPeter Penzov 1,628156 gold badges501 silver badges908 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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());
发布评论

评论列表(0)

  1. 暂无评论