我在控制器上有一個方法來獲取聊天室所有懲罰型別的串列(踢、禁止、警告和靜音)。在我模擬資料的第一個測驗中,它按預期作業并且測驗通過。
但是,在我提供的第二次測驗中。我定義了應該回傳的Optional<Punishment>內容,懲罰名稱的屬性設定為“靜音”。我很困惑為什么這給了我null。當我在測驗之外運行 Spring 應用程式時,路由作業正常。出于某種原因,模擬從不想回傳我指定的值,而只是回傳 null。具體來說,這被在線測驗中捕獲,.andExpect(jsonPath("$.punishmentName", Matchers.equalTo("mute")));因為欄位值為 null 給出以下錯誤:
java.lang.AssertionError: No value at JSON path "$.punishmentName"
為了清楚起見,我還提供了控制器方法和服務方法。
懲罰控制器測驗:
@WebMvcTest(PunishmentController.class)
@RunWith(SpringRunner.class)
public class PunishmentControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private PunishmentService punishmentService;
@MockBean
private PunishmentValidator punishmentValidator;
@Test
public void getAllPunishmentTypesReturnsAListOfPunishmentTypes() throws Exception {
List<Punishment> punishments = new ArrayList<>();
punishments.add(new Punishment("mute"));
punishments.add(new Punishment("kick"));
punishments.add(new Punishment("ban"));
punishments.add(new Punishment("warn"));
Mockito.when(punishmentService.getAllPunishmentTypes()).thenReturn(punishments);
mvc.perform(get("/api/punishments"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.hasSize(4)))
.andExpect(jsonPath("$[0].punishmentName", Matchers.equalTo("mute")))
.andExpect(jsonPath("$[1].punishmentName", Matchers.equalTo("kick")))
.andExpect(jsonPath("$[2].punishmentName", Matchers.equalTo("ban")))
.andExpect(jsonPath("$[3].punishmentName", Matchers.equalTo("warn")));
}
@Test
public void getPunishmentTypeReturnsMuteWhenMuteIsSpecified() throws Exception {
Optional<Punishment> mute = Optional.of(new Punishment("mute"));
Mockito.when(punishmentService.getPunishmentType("mute")).thenReturn(mute);
mvc.perform(get("/api/punishments/mute"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.punishmentName", Matchers.equalTo("mute")));
}
控制器方法:
/**
* GET request for all punishment types.
* @return List<Punishment> - When Punishments are found in the database they are returned in a List object.
* Otherwise, an empty list is returned if no records are found or an error occurs.
*/
@GetMapping
public List<Punishment> getAllPunishments() {
return punishmentService.getAllPunishmentTypes();
}
/**
* GET request for one punishment type.
* @param punishmentType String - The type of punishment.
* @return Optional<Punishment> - The rule that gets returned or an empty optional if no rule is found.
*/
@GetMapping(path = "{punishmentType}")
public Optional<Punishment> getPunishment(@PathVariable("punishmentType") String punishmentType) {
boolean isPunishmentTypeValid = punishmentValidator.validatePunishmentName(punishmentType);
if (isPunishmentTypeValid) {
return punishmentService.getPunishmentType(punishmentType);
} else {
return Optional.empty();
}
}
}
服務方式:
/**
* Gets all the punishment types
* @return List<Punishment> - The rules in the community
*/
public List<Punishment> getAllPunishmentTypes() {
return punishmentRepository.findAll();
}
/**
* Gets a specific punishment type.
* @param punishmentType String - The type of punishment.
* @return The punishment retrieved.
*/
public Optional<Punishment> getPunishmentType(String punishmentType) {
return punishmentRepository.findById(punishmentType);
}
uj5u.com熱心網友回復:
我相信這是因為您忘記模擬PunishmentValidator#validatePunishmentName("mute")要回傳true的方法,從而PunishmentService永遠不會呼叫您存根的方法,因為默認情況下,如果您不存根方法,它將回傳 false(請參閱this)。
此外,它是一種已知行為,@MockBean被配置為寬松的存根UnnecessaryStubbingException,如果您存根一個方法但它實際上沒有被執行,它不會報告錯誤(即 throw )。
因此,更改以下內容應該可以解決您的問題:
@Test
public void getPunishmentTypeReturnsMuteWhenMuteIsSpecified() throws Exception {
Optional<Punishment> mute = Optional.of(new Punishment("mute"));
Mockito.when(punishmentService.getPunishmentType("mute")).thenReturn(mute);
Mockito.when(punishmentValidator.validatePunishmentName("mute")).thenReturn(true);
mvc.perform(get("/api/punishments/mute"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.punishmentName", Matchers.equalTo("mute")));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425494.html
上一篇:SpringWebMVC與SpringWebFlux。阻塞和非阻塞
下一篇:如果應用程式在SpringMVC中關閉并再次啟動,是否會在WebApplicationContext中創建一個新的Singleton范圍物件實體
