英文:
Mock a service to return an empty list to test 404 status code
问题
我正在尝试测试此REST服务,以便在`itemService.getActiveItems()`返回空`List`时抛出NOT_FOUND(404)错误:
@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("/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);
    }
}
I use the TestRestTemplate to test :
     @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);
        }
I'm trying to cause the "/items/active" endpoint to return new ResponseEntity<>(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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论