Mocked FormFactory for controllers tests returns empty form instance. (play 2.6)

225 views
Skip to first unread message

Aleksandr Savvopulo

unread,
May 23, 2017, 9:07:35 AM5/23/17
to Play Framework
Hello, I am trying to write tests for controllers and need to mock (with mockito) FormFactory. 


Test code:
MessagesApi messagesApi = mock(MessagesApi.class);
Validator validator = mock(Validator.class);
FormFactory formFactory = new FormFactory(messagesApi, new Formatters(messagesApi), validator);
Playground controller = new Playground(formFactory);

So controller gets instance of form factory.
I am binding form from request as: 
Form<MyForm> form = formFactory.form(MyForm.class).bindFromRequest();

And produced form contains all data I sent in 'data' field, but if I get the serialized form as object via form.get(), 
I see that all fields in the MyForm instance is null.

So form factory able to read the data from the request, but for some reason can't fill the instance with the values. 


Do you know, what is the reason and how to solve this? 

Thanks.


Aleksandr Savvopulo

unread,
May 23, 2017, 11:05:57 AM5/23/17
to Play Framework
It is like the main issue is instantiating a validator. Could you help how to do that correctly? 
(with regards that I don't have injector in tests.)

Will Sargent

unread,
May 23, 2017, 1:53:19 PM5/23/17
to play-fr...@googlegroups.com
Do you want something like this?

public class JavaFormsModule {

    @Singleton
    @Provides
    public FormFactory providesFormFactory(MessagesApi messagesApi, Formatters formatters, ValidatorProvider validator) {
        return new FormFactory(messagesApi, formatters, validator.get());
    }

    @Singleton
    @Provides
    public Formatters providesFormatters(MessagesApi messagesApi) {
        return new Formatters(messagesApi);
    }

    @Singleton
    @Provides
    public ValidatorProvider providesValidator(ConstraintValidatorFactory factory) {
        return new ValidatorProvider(factory);
    }

    @Singleton
    @Provides
    public ConstraintValidatorFactory constraintValidatorFactory(Injector injector) {
        return new DefaultConstraintValidatorFactory(injector);
    }
}

--
Will Sargent
Engineer, Lightbend, Inc.


On Tue, May 23, 2017 at 8:05 AM, Aleksandr Savvopulo <pro10...@gmail.com> wrote:
It is like the main issue is instantiating a validator. Could you help how to do that correctly? 
(with regards that I don't have injector in tests.)

--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/edf7867e-e03c-4691-87f3-c6578ad29cb2%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Aleksandr Savvopulo

unread,
May 24, 2017, 2:25:06 AM5/24/17
to Play Framework
Yes, but it is like I don't have Injector in unit tests. 
In other case I have to extend WithApplication. 
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.

Aleksandr Savvopulo

unread,
May 24, 2017, 10:40:35 AM5/24/17
to Play Framework
I found only following way how to get working FormFactory: 

Application app = new GuiceApplicationBuilder()
 
.disable(DBModule.class, JPAModule.class)
       
.build();

FormFactory formFactory = app.injector().instanceOf(FormFactory.class);

In this way I can provide working form factory to controller, and have request payload mapping working. 

But I wonder, is there any faster way to get working FormFactory instance?
Should I extend from WithApplication class? 

Will Sargent

unread,
May 24, 2017, 12:53:54 PM5/24/17
to play-fr...@googlegroups.com
Yes, you should extend WithApplication:


and then provide a method:

FormFactory formFactory() {
 return app.injector().instanceOf(FormFactory.class);
}




--
Will Sargent
Engineer, Lightbend, Inc.


--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/ffda3350-f96d-4303-8409-e55ba59a7187%40googlegroups.com.

Rajashree Gr

unread,
Jun 20, 2017, 1:10:44 PM6/20/17
to Play Framework
How to get this GuiceApplicationBuilder? I'm not able to get this dependency in SBT. 

Rajashree Gr

unread,
Jun 20, 2017, 2:23:32 PM6/20/17
to Play Framework
I managed to get it finally. "com.typesafe.play" % "play-java_2.11" % "2.5.8"
Reply all
Reply to author
Forward
0 new messages