> For the complete documentation index, see [llms.txt](https://olavo-moreira.gitbook.io/studies/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://olavo-moreira.gitbook.io/studies/testes-automatizados-junit5-e-mockito/testes-de-unidade/service/findbyid-existente-e-nao-existente.md).

# findById (existente e não existente)

Comportamento simulado

<figure><img src="https://1454409036-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeQn9Sq05y05L1GUxOouc%2Fuploads%2F784PUpCIdUn7EpywLLCt%2Fimage.png?alt=media&amp;token=3c00844f-11a0-4254-a9e2-ced89265f9db" alt=""><figcaption></figcaption></figure>

ID existente:

```java
@Test
public void findByIdShouldReturnProductDtoWhenIdExists() {
    ProductDTO result = productService.findById(existingId);

    Assertions.assertNotNull(result);
}
```

ID não existente:

```java
@Test
public void findByIdShouldThrowResourceNotFoundExceptionWhenIdDoesntExist() {
    Assertions.assertThrows(ResourceNotFoundException.class, () -> {
       productService.findById(nonExistingId);
    });
}
```
