• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Autowiring does not work inside custom constraint validator using Spring-Test-MVC

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eveyone,

I have posted this in the Spring forums but have not had a reply. Can anyone help with this please..?

I have an issue with autowiring inside a custom constraint validator when running an integration test using spring-test-mvc. The service I require is null.

However when I run the web app, its is auotwired correctly.

Custom Constraint Validator (Booking Service is null!)



Annotation


Service


Unit Test



Config



Controller


 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

With Thanks to Rossen Stoyanchev I have this working.

In short the problem is to do with standloneSetup with does not load spring configuration.

One option is to create a LocalValidatorFactoryBean configured with a SpringConstraintValidatorFactory and plug it into the standaloneSetup builder.

However I decided to migrate from spring-test-mvc to spring-test in 3.2.

All that was required was to annotate my test class with @WebAppConfiguration, load in my spring configuration files with @ContextConfiguration and then @Autowired WebApplicationContext

Example




I can now @Autowire services in my custom jsr annotation validator.

Hope this helps others

Regards
Darren C.


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We also faced the similar problem where @Autowiring was failing (not initialised) in ConstrainValidator Class. Our ConstraintValidator Implemented class was using a value which supposed to be read from the `application.yml` file.  Below solution helped us as this is using a pure spring scope. Hope this helps, with proper SpringJunit4ClassRunner.
 
The overall mission is to change the world. When you've done that, then you can read this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!