首先-我已經檢查了有關此問題的所有先前主題,但沒有一個有幫助。
具有以下代碼:
@DisplayName("GET RecipeUltraLight by id is successful")
@Test
public void givenRecipeId_whenGetRecipeDetailsById_thenReturnRecipeObject(){
// given
given(this.recipeRepository.findById(recipe.getId())).willReturn(Optional.of(recipe));
given(this.recipeService.getRecipeById(recipe.getId())).willReturn(recipe);
given(this.recipeConverter.toUltraLight(recipe)).willReturn(recipeUltraLightDto);
// when
RecipeUltraLightDto retrievedRecipe = recipeService.getRecipeUltraLightById(recipe.getId());
// then
verify(recipeRepository, times(1)).findById(recipe.getId());
verify(recipeService, times(1)).getRecipeById(recipe.getId());
verify(recipeConverter, times(1)).toUltraLight(recipe);
assertThat(retrievedRecipe).isNotNull();
}
給我這個錯誤:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Recipe cannot be returned by findById()
findById() should return Optional
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
服務方式:
@Transactional(readOnly = true)
public RecipeUltraLightDto getRecipeUltraLightById(Long id) {
Recipe recipe = getRecipeById(id);
RecipeUltraLightDto dto = new RecipeUltraLightDto();
dto = recipeConverter.toUltraLight(recipe);
return dto;
}
// internal use only
@Transactional(readOnly = true)
public Recipe getRecipeById(Long id) {
if (id == null || id < 1) {
return null;
}
return recipeRepository.findById(id)
.orElseThrow(() -> new RecipeNotFoundException(
String.format("Recipe with id %d not found.", id)
));
}
設定:
@ContextConfiguration(classes = {RecipeService.class})
@ExtendWith({SpringExtension.class, MockitoExtension.class})
class RecipeServiceTest {
@MockBean
private RecipeConverter recipeConverter;
@MockBean
private RecipeRepository recipeRepository;
@Autowired
private RecipeService recipeService;
private Recipe recipe;
private RecipeUltraLightDto recipeUltraLightDto;
@BeforeEach
public void setup(){
recipe = Recipe.builder()
.id(1L)
.name("Recipe")
.description("Description")
.createdAt(LocalDateTime.now())
.difficulty(RecipeDifficulty.EASY)
.minutesRequired(60)
.portions(4)
.authorId(1L)
.views(0)
.isVerified(false)
.build();
recipeUltraLightDto = RecipeUltraLightDto.builder()
.id(1L)
.name("Recipe")
.build();
}
我試過了:
- 可選的.ofNullable()
- 添加 .isPresent()
- 擺脫 .orElseThrow 并通過 if 陳述句并使用 .get()
- 科特林
如果有人可以提供幫助,我會很高興。
uj5u.com熱心網友回復:
您正在創建您正在測驗的物件的模擬,并且基本上也使存盤庫的模擬無用。
您應該洗掉該行given(this.recipeService.getRecipeById(recipe.getId())).willReturn(recipe);
,這樣它只會呼叫方法并呼叫存盤庫。現在將回傳模擬結果。因為那是現在開始的行為。
uj5u.com熱心網友回復:
明確提到了findById()
回傳的方法Optional
,需要Recipe
通過呼叫來獲取Optional.get()
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/508594.html
上一篇:為依賴函式撰寫單元測驗