我正在尝试为在“if”中测试 2 个值的方法实现单元测试。我怎样才能做到这一点 ?
对于 valService.validate 部分,我调用 when.thenReturn 并且它工作正常,但是对于 equals 部分,我的测试无法变绿,当我调试时,我发现它没有通过 if 的第一部分,并且userTypeSpecific 具有空值。
编辑:我从 app.properties 中获取带有 @value 的 userTypeSpecific 变量
我的控制器
@Value("${app.usertype.specific}")
String userTypeSpecific;
@PostMapping(value = "decline")
@ResponseBody
public ResponseEntity<Boolean> declineUser(@RequestParam final String idUser, @RequestHeader("userType") String userType) {
HttpStatus httpStatus = HttpStatus.FORBIDDEN;
Boolean result = false;
if (userTypeSpecific.equals(userType) && valService.validate(idUser, userType)) {
result = this.service.declineUser(idUser);
if (result){
httpStatus = HttpStatus.OK;
}
}
return ResponseEntity.status(httpStatus).body(result);
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试代码:
@SpringBootTest
@ExtendWith(SpringExtension.class)
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = {"app.usertype.specific=spec",})
public class UserControllerTest { …Run Code Online (Sandbox Code Playgroud)