'LoginController'에 'passwordActive'작업에 대한 단위 테스트 케이스를 작성하려고했습니다. 그러나 나는 모든 것을 조롱 당하고 그것을 어떻게 할 지 확신하지 못합니다.Grails의 Unit 테스트 케이스에서 메소드를 조롱했습니다.
def passwordActive={
log.error("Entering into Password Active")
println "Password Active -- Params = "+params
String contextPath = request.contextPath
String username = null
User user = null
try{
if (params.username != null && params.username.trim().length() > 0) {
username = params.username
user = User.findByUsername(username)
}
if (user == null) {
log.error("USER not found.")
flash.errorLogin = "Username is incorrect"
redirect(action:"auth")
return
}else if(user.password != params.password){
log.error("USER password is wrong..")
flash.errorLogin = "Please enter valid password.."
redirect(action:"auth")
return
}else if (!user.active) {
log.error("USER is inactive.")
flash.errorLogin = "User in Inactive"
redirect(action:"auth")
return
}
session["userid"] = user.id
String userRole = user.authority.authority
String url = contextPath + "/" + userRole
println "URL = "+url
redirect(url: url, method: 'post', params: params)
return
}
catch(Exception e){
println e.printStackTrace()
}
log.error("Exit into Password Active")
}
사용자 이름이없는 테스트 사례를 작성하고 싶습니다. 이 경우 플래시 메시지에 "username incorrect"라는 메시지가 있어야하며이를 주장 할 수 있습니다. 여기 모든 것을 조롱해야하고 어떻게해야하는지. 작은 예제로 작업을 설명해 주시겠습니까? (위의 코드와 관련하여 선호하면 이해하기 쉽습니다)
다음과 같이 서비스 클래스에서 메소드를 조롱하려고했습니다. 하지만 작동하지 데프 userService controller.userService = 새로운 UserService() controller.userService = [passwordComplexityCheck은 : {데프 K-> 반환 사실 }] 다음은 내 서비스 클래스 UserService 클래스는 { // 필요 선언 입니다 ........ public boolean passwordComplexityCheck (String password) {return true}} –
어떤 방식으로 작동하지 않습니까? 실제 메서드가 호출되는지 또는 어떤 종류의 예외가 발생합니까? 그렇다면 예외는 무엇입니까? –