Grails unit testing service with spock mocking functionality - unit-testing

Presently trying to test a Grails service (Experience with Geb functional testing)
Trying to mock the necessary required data that the service will need (e.g. User,etc.) ... no matter how I seem to declare/initialize the domains of interest they all appear as NULL in all my tests methods
I'm trying to set this required information once, so it can be reused through the test methods.
#TestFor(ReminderService)
#Mock([Reminder, User, Subscriptions, Organisation, UserOrganisation, OrganisationRole, Ref, Role, Title])
class ReminderServiceTests extends Specification {
#Shared user, org, userOrg, roleUser, sub, orgRole, ti, refVals, reminder
def mailService
def setup() {
def refSubStatus = new Ref(value: 'Current').save()
def refSubPublic = new Ref(value: 'No').save()
def refSubType = new Ref(value: 'Subscription Taken').save()
def refRemUnit = new Ref(value: 'Month').save()
def refOrgRole = new Ref(value: 'Subscriber').save()
def refRemMethod = new Ref(value: 'email').save()
def refRemTrigger = new Ref(value: 'Subscription Manual Renewal Date').save()
reminder = new Reminder(user: user, active: true, amount: 1, unit:refRemUnit, reminderMethod:refRemMethod, trigger: refRemTrigger, lastRan:null).save()
refVals = [refSubStatus,refSubPublic, refSubType, refRemUnit, refOrgRole, refRemMethod, refRemTrigger]
roleUser = new Role(authority: 'BASIC_USER', roleType:'global').save()
ti = new Title(title: "A random title....", impId: UUID.randomUUID().toString()).save()
sub = new Subscription(name:"A random subscription name",
status:refSubStatus,
identifier:UUID.randomUUID().toString(),
impId:UUID.randomUUID().toString(),
startDate:new LocalDate().minusYears(1).toDate(),
endDate: new LocalDate().plusMonths(1).toDate(),
isPublic: refSubPublic,
type: refSubType,
renewal: new LocalDate().minusMonths(3).toDate()).save()
org = new Organisation(name: "new org", impId: UUID.randomUUID().toString()).save()
orgRole = new OrganisationRole(sub: sub, roleType: refOrgRole, org: org).save()
user = new User(username: 'j_doe', firstname: "John", lastname: "Doe", email: 'example#googlemail.com', defaultDash: org).save()
userOrg = new UserOrganisation(org: org, user: user, formalRole: roleUser, status: 1).save()
mailService = new MailService()
// mockDomain(Ref, refdataValues)
// mockDomain(Title, ti)
// mockDomain(OrganisationRole, orgRole)
// mockDomain(Organisation, org)
// mockDomain(User, user)
// mockDomain(UserOrganisation, userOrg)
// mockDomain(Reminder, reminder)
}
def "Getting subscriptions for a user"() {
when:
def subscriptions = service.getAuthorisedSubsciptionsByUser(user)
then:
subscriptions != null
subscriptions.size() > 0
}
Everything is null, I've tried using mockDomain (See commented out section in setup(), including using setupSpec() something is not quite right)

Have you tried not using #Shared? AS I see it now, you don't need it. Your references are shared throughout the lifecycle of the Spec, but you put your init code in setup, which is called before each spec. That does not seem right. #Shared variables are internally stored in a different instance than the normal variables, so there may be some kind of mixup. Also, you might try to use setupSpec() instead of setup() for #Shared variables.

#TestFor(SomeService)
#Mock([DomainClass])
class SomeServiceSpec extends Specification {
def "test someMethod"() {
given:
def x = new DomainClass(attr1: 'a', attr2: 'b').save(flush: true)
when:
def result = service.someMethod() // returns that saved Domain instance
then:
result instanceof DomainClass
result.attr1 == 'a'
result.attr2 == 'b'
}
}
.save() has proven insufficient in my unit tests, while .save(flush: true) produces the desired results.

Related

How to mock passwordEncoder using Spock in a Grails unit test

I could use some advice in how to mock an auto wired dependency used in a Grails unit test. I've omitted most of the unnecessary code and just given the test class and the relevant methods in the file class under test
class UserService {
def springSecurityService // spring bean
def passwordEncoder // auto wired as per
//https://stackoverflow.com/questions/33303585/spring-//security-encode-password-with- bcrypt-algorithm
.....
def passwordPreviouslyUsed(String newPassword, def userId){
def passwordExists = false
def usersPasswords = findPasswordsForUser(userId)
usersPasswords.each{ password ->
if (passwordEncoder.isPasswordValid(oldPassword, newPassword, null)) {
passwordExists = true
}
}
return passwordExists
}
.....
def findPasswordsForUser(def userId){
User foundUser = User.findById(userId)
def passwordsForUser = UserPasswords.createCriteria().list {
eq('user', foundUser)
projections{
property('password')
}
}
passwordsForUser
}
My test
class UserServiceSpec extends Specification implements DataTest, ServiceUnitTest<UserService> {
def passwordEncoder
def setupSpec() {
mockDomains User, UserPasswords
}
def setup() {
def stubPasswordEncoder = Stub(passwordEncoder) {
isPasswordValid(_, _, _) >> true
}
service.passwordEncoder = stubPasswordEncoder
}
void "test for user passwordPreviouslyUsed"() {
given: "a user already exists"
setup()
service.createNewUser("testName", "testy#test.com", "Secret1234" )
//^(does some validation, then User.save())
User foundUser = User.findByEmail("testy#test.com")
foundUser.fullName == "testName"
long testUserId = foundUser.id
and: "we update the password for that user, and it to the userPasswords"
UserPasswords newUserPassword = new UserPasswords(
user: foundUser,
password: "newPassword1"
)
newUserPassword.save()
//use passwordPreviouslyUsed method to check a string with the same value as the
//previously
//updated password to check if it has already been used
when: "we check if the password has been used before"
def response = service.passwordPreviouslyUsed("newPassword1", fundsyId)
then:
response == true
}
Without stubbing or mocking this dependency, I get the error
Cannot invoke method isPasswordValid() on null object
I tried to stub password encoder and have it return true
def stubPasswordEncoder = Stub(passwordEncoder) {
isPasswordValid(_, _, _) >> true
}
service.passwordEncoder = stubPasswordEncoder
But this gives an error message:
Stub in 'spock.mock.MockingApi' cannot be applied to '(java.lang.Object, groovy.lang.Closure)'
Is there any way to mock this dependency with Spock?
Stub and Mock take a class - you're giving it an instance that is null - hence the exception.
You should be able to mock it as so:
def mockPasswordEncoder = Mock(PasswordEncoder)
// note this is the class
// org.springframework.security.crypto.password.PasswordEncoder
I tried enrichelgeson's approach, and it worked!
I first imported PasswordEncoder to the test class
import org.springframework.security.crypto.password.PasswordEncoder
then implemented the normal mocking procedure. I was initially confused because they class under test just implicitly created an instance of the class by defining it.
def stubPasswordEncoder = Stub(PasswordEncoder) {
isPasswordValid(_, _, _) >> true
}
service.passwordEncoder = stubPasswordEncoder
I also found another solution that didn't require mocking
service.passwordEncoder = [ isPasswordValid: { String rawPass, String salt, Null -> true } ]
Both approaches work fine. Thanks for the help!

How to get the currentUser provided by spring security in Grails 2 unit testing

Hi guys i am on trouble about getting the current user provided by spring.
Here's my unit test code
void "Test if adding project will sucess"() {
given:
def createProjectMock = mockFor(UserService)
createProjectMock.demand.createNewProject { Map projectMap ->
return true
}
controller.userService = createProjectMock.createMock()
when: "saveProject is execute"
controller.saveProject()
then: "page will to the list to view the saved project"
response.redirectedUrl == '/user/index2'
}
Here's my controller
def saveProject(ProjectActionCommand projectCmd) {
def currentUser = springSecurityService.currentUser
if (projectCmd.hasErrors()) {
render view: 'createProject', model: [projectInstance: projectCmd, user:currentUser]
} else {
def getProjectMap = [:]
getProjectMap = [
projectName: params.projectName,
user: currentUser
]
def saveProject = userService.createNewProject(getProjectMap)
if (saveProject) {
redirect view: 'index2'
} else {
render 'Error upon saving'
}
}
}
And here's my service
Project createNewProject(Map projectMap){
def createProject = new Project()
createProject.with {
projectName = projectMap.projectName
user = projectMap.user
}
createProject.save(failOnError:true, flush: true)
}
And i always getting this error:
Cannot get property 'currentUser' on null object.
Hope you can help me. Thanks
Cannot get property 'currentUser' on null object.
means that you haven't mocked springSecurityService. Let's do it in setup section (I assume it may be useful also in other methods in this class):
def springSecurityService
def setup() {
springSecurityService = Mock(SpringSecurityService)
controller.springSecurityService = springSecurityService
}
At this point your code is going to work. However remember that you can always mock also the actual logged user and test it at any point:
User user = Mock(User)
springSecurityService.currentUser >> user

How to mock springSecurityService in an unit test

I am unit testing a Grails controller method that internally creates an user instance. User domain class uses the springSecurityService of the Spring Security plugin to encode the password before inserting it into the database.
Is there a way to mock that springSecurityService from my unit test in order to get rid of that error?
Failure: Create new individual member(MemberControllerSpec)
| java.lang.NullPointerException: Cannot invoke method encodePassword() on null object
Please find my unit test below.
#TestMixin(HibernateTestMixin)
#TestFor(MemberController)
#Domain([User, IndividualPerson])
class MemberControllerSpec extends Specification {
void "Create new individual member"() {
given:
UserDetailsService userDetailsService = Mock(UserDetailsService)
controller.userDetailsService = userDetailsService
def command = new IndividualPersonCommand()
command.username = 'scott#tiger.org'
command.password = 'What ever'
command.firstname = 'Scott'
command.lastname = 'Tiger'
command.dob = new Date()
command.email = command.username
command.phone = '89348'
command.street = 'A Street'
command.housenumber = '2'
command.postcode = '8888'
command.city = 'A City'
when:
request.method = 'POST'
controller.updateIndividualInstance(command)
then:
view == 'createInstance'
and:
1 * userDetailsService.loadUserByUsername(command.username) >> null
and:
IndividualPerson.count() == 1
and:
User.count() == 1
cleanup:
IndividualPerson.findAll()*.delete()
User.findAll()*.delete()
}
}
One way to mock a service is to use Groovy's MetaClass
import grails.test.mixin.Mock
import grails.plugin.springsecurity.SpringSecurityService
...
#Mock(SpringSecurityService)
class MemberControllerSpec extends Specification {
def setupSpec() {
SpringSecurityService.metaClass.encodePassword = { password -> password }
}
def cleanupSpec() {
SpringSecurityService.metaClass = null
}
....
In this example, the call to SpringSecurityService.encodePassword() will simply return the password in plain text.
An approach using Mocks is discussed here.
You can to use this code to encode password in User:
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
}
When springSecurityService is null, encodePassword is not called and NPE is not raised
When you use controller unit test with spring security rest plugin in Grails v4/v3, if your controller method reference springSecurityService methods like 'athenticatedUser', there will be NullPointException, because springSecurityService is not autowired into the spring application context.
Add code like below, you can inject springSecurityService and mock it's methods.
class GuessControllerSpec extends Specification implements ControllerUnitTest<GuessController> {
#Override
Closure doWithSpring() {
return {
// mock method
SpringSecurityService.metaClass.getCurrentUser = {return new User()}
// inject into spring context
springSecurityService(SpringSecurityService)
}
}
...
}

Unit Test Grails with Service

I'm trying to do a test on controller that uses 3 Domains and a service to perform persistence, when I use these values ​​in view he is saving normally, but in my unit test does not pass validation, I do not understand why . If someone who has been there can help me, I do not know if the Mock I'm doing is correct, I followed the examples in the documentation oficial .
thats the error message:
junit.framework.AssertionFailedError: expected:<1> but was:<0>
Thats my code for Test:
#TestFor(UsuarioController)
#Mock([SecRole, UsuarioService, Usuario, Cliente, Secuser])
#TestMixin(ControllerUnitTestMixin)
class UsuarioTests {
private def usuarioCommand
private def service
#Before
void setUp() {
usuarioCommand = mockCommandObject(UsuarioCommand)
service = mockFor(UsuarioService)
}
#Test
void testCadastrarUsuarioCorreto() {
usuarioCommand.perfil = 2
usuarioCommand.nome = "Michael Swaltz"
usuarioCommand.cpf = "381.453.718-13"
usuarioCommand.email = "michael.s#mail.com"
usuarioCommand.login = "login"
usuarioCommand.senha = "senha"
usuarioCommand.senhaRepeat = "senha"
assertTrue( usuarioCommand.validate() );
controller.usuarioService = service
controller.create(usuarioCommand)
assertEquals(1, Usuario.count())
}
This is my controller action:
def create = { UsuarioCommand usuario ->
if(!params.create) return
if(!usuario.hasErrors()) {
def secuser = new Secuser(username: usuario.login, password: usuario.senha, senha: usuario.senhaRepeat, enabled: true)
def user = new Usuario(nomeUsuario: usuario.nome, email: usuario.email, cpf: usuario.cpf, idNivelAcesso: usuario.perfil)
def cliente = Cliente.findByUsuario( session.usuario )
user.setIdCliente(cliente)
def secrole = SecRole.get( usuario.perfil )
try{
usuarioService.save(user, secuser, secrole)
flash.message = "Usuário ${usuario.nome} cadastrado.".toString()
redirect (action: 'list')
}catch(ValidationException ex) {
StringBuilder mensagem = new StringBuilder();
ex.errors.fieldErrors.each { FieldError field ->
mensagem.append("O campo ").append( field.field )
.append(" da classe ")
.append( field.objectName )
.append(" com o valor ")
.append( field.rejectedValue )
.append(" não passou na validação.")
.append("\n")
}
flash.error = mensagem.toString()
return [usr: usuario]
}catch(ex) {
flash.error = ex.message
render "Erro"
//return [usr: usuario]
}
}else {
usuario.errors.allErrors.each { println it }
render "Erro"
//return [usr: usuario]
}
}
mockFor would give you back a mock control. You have to explicitly call createMock() on the mock control to get the actual mocked object.
service = mockFor(UsuarioService).createMock()
Have a look at "Mocking Collaborators" from the same link you referred. The test can be optimized if you still face an issue.
similar example and one here.
You need to set an expectation on the UsarioService to say what will be returned when usarioService.save(...) is called.
Before getting to that point, you need to say in the test
mockUsarioService.createMock() which will create the actual instance of the mock object, that's what you will pass to the controller usarioService attribute. Copied the code below from the Grails documenation. http://grails.org/doc/1.1/guide/9.%20Testing.html
String testId = "NH-12347686"
def otherControl = mockFor(OtherService)
otherControl.demand.newIdentifier(1..1) {-> return testId }
// Initialise the service and test the target method.
def testService = new MyService()
testService.otherService = otherControl.createMock()

Unit testing custom validator of command object with dependency

I have a command object for registering user, and I want to check how old is the user. This command object has a service dependency. How can I test custom validator for my dateOfBirth property? As it looks now is taken straight from documentation, here.
class RegisterUserCommand {
def someService
String username
String password
String password2
String email
Date dateOfBirth
static constraints = {
// other constraints
dateOfBirth blank: false, validator: {val, obj ->
return obj.someService.calculateAge(val) >= 18
}
}
So basically the question is: how can I mock 'obj' parameter of the validator closure?
The easiest way to test validation on a command object is to use GrailsUnitTestCase.mockForConstraintsTests. A mock validate method will be applied to your command object, and you can just call validate() like you would outside of a test.
Here's an example of how you could write your unit test. The blank constraint isn't meaningful for dates, so I've changed it to nullable: false.
import grails.test.GrailsUnitTestCase
class RegisterUserCommandTests extends GrailsUnitTestCase {
RegisterUserCommand cmd
protected void setUp() {
super.setUp()
cmd = new RegisterUserCommand()
mockForConstraintsTests RegisterUserCommand, [cmd]
}
void testConstraintsNull() {
cmd.dateOfBirth = null
cmd.someService = [calculateAge: { dob -> 18 }]
def result = cmd.validate()
assert result == false
assert cmd.errors.getFieldErrors('dateOfBirth').code == ['nullable']
}
void testConstraintsCustom() {
cmd.dateOfBirth = new Date()
cmd.someService = [calculateAge: { dob -> 17 }]
def result = cmd.validate()
assert result == false
assert cmd.errors.getFieldErrors('dateOfBirth').code == ['validator.invalid']
}
}
Note that your service won't get injected in a unit test (it will in an integration test though), so you'll either need to mock it, as above, or create an instance and assign it to cmd.someservice.