模拟一个服务返回空列表以测试404状态码。

huangapple go评论82阅读模式
英文:

Mock a service to return an empty list to test 404 status code

问题

我正在尝试测试此REST服务以便在`itemService.getActiveItems()`返回空`List`时抛出NOT_FOUND404错误

@GetMapping("/items/active")
public ResponseEntity<List<ItemEntity>> getActiveItems() {

    List<ItemEntity> activeItems = itemService.getActiveItems();
    if (activeItems.isEmpty()) {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } else {
        return new ResponseEntity<>(activeItems, HttpStatus.OK);
    }
}

我使用TestRestTemplate进行测试

@Mock
private ItemService itemService;

@Test
public void activeItemsTest() {
    private TestRestTemplate templateAuth;

    when(itemService.getActiveItems()).thenReturn(new ArrayList());

    ResponseEntity<List<ItemEntity>> result = templateAuth.exchange("/items/active", HttpMethod.GET,
            null, new ParameterizedTypeReference<List<ItemEntity>>() {
            });
    Assertions.assertThat(result.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}

我试图使"/items/active"端点返回`new ResponseEntity<>(HttpStatus.NOT_FOUND)`,因为我已经模拟了`itemService.getActiveItems()`以返回一个空的`ArrayList`。当我测试`result.getStatusCode()`没有包含404我应如何修改我的测试以返回404错误代码
英文:

I've attempting to test this REST service such that a NOT_FOUND (404) error is thrown when itemService.getActiveItems() returns an empty List:

@GetMapping(&quot;/items/active&quot;)
public ResponseEntity&lt;List&lt;ItemEntity&gt;&gt; getActiveItems() {

    List&lt;ItemEntity&gt; activeItems = itemService.getActiveItems();
    if (activeItems.isEmpty()) {
        return new ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND);
    } else {
        return new ResponseEntity&lt;&gt;(activeItems, HttpStatus.OK);
    }
}

I use the TestRestTemplate to test :

     @Mock
     private ItemService itemService;
    
            @Test
            public void activeItemsTest() {
            private TestRestTemplate templateAuth;
        
        when(itemService.getActiveItems()).thenReturn(new ArrayList());
            
                    ResponseEntity&lt;List&lt;ItemEntity&gt;&gt; result = templateAuth.exchange(&quot;/items/active&quot;, HttpMethod.GET,
                            null, new ParameterizedTypeReference&lt;List&lt;ItemEntity&gt;&gt;() {
                            });
                    Assertions.assertThat(result.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
        }

I'm trying to cause the &quot;/items/active&quot; endpoint to return new ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND); as I have mocked itemService.getActiveItems() to return an empty ArrayList. When I test result.getStatusCode() does not contain 404. How should I amend my test in order to return a 404 error code ?

答案1

得分: 1

你可以使用JUnit 5,并在类上注释@ExtendWith(MockitoExtension.class)@SpringBootTest。然后将

 @Mock
 private ItemService itemService;

更改为

 @SpyBean
 private ItemService itemService;

这样,您可以模拟注入到控制器中的服务的行为。

英文:

You could use JUnit 5 and annotate the class with @ExtendWith(MockitoExtension.class)
and @SpringBootTest. Then change

 @Mock
 private ItemService itemService;

to

 @SpyBean
 private ItemService itemService;

so that you can mock the behaviour of the service that is autowired into the Controller

huangapple
  • 本文由 发表于 2020年9月17日 01:58:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/63925555.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定