0

We have Controllers with dynamic request mapping:

(@RequestMapping(method = RequestMethod.POST, value = "${api.emailverification.officialvalidate.POST.uri}")

These request mappings are resolved via properties file in spring Environment.

Now, in our SpringJUnit4ClassRunner test class's setup method, we are trying to mockmvc:

MockMvcBuilders.standaloneSetup(controllerUnderTest).build()

But MockMvcBuilder is not able to resolve value = "${api.emailverification.officialvalidate.POST.uri}", though Properties file is present in spring environment. Below is actual code:

Controller method:

@RequestMapping(method = RequestMethod.POST, value = "${api.emailverification.officialvalidate.POST.uri}"
            ,consumes =  MediaType.APPLICATION_JSON_VALUE, produces =  MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ResponseBean> captureDataAndValidateOfficialEmailLink(HttpServletRequest request,@Valid @RequestBody CaptureDataRequest reqBean,BindingResult result,@RequestHeader HttpHeaders headers) 
    {
.....}

Junit class:

@SuppressWarnings("deprecation")
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
public class SendVerificationMailControllerTest 
{
    @Autowired
    ApplicationContext context;

    @Before
    public void setUp() throws Exception
    {.....
        ((ConfigurableEnvironment)context.getEnvironment()).getPropertySources().addFirst(new MockPropertySource("centralconfig",prop));
        this.mockMvc = MockMvcBuilders.standaloneSetup(sendVerificationMailController).setControllerAdvice(exceptioHandler).build();
......
}

Ali
  • 3,373
  • 5
  • 42
  • 54
  • Below code works fine: this.mockMvc = MockMvcBuilders.standaloneSetup(sendVerificationMailController).addPlaceholderValue("api.emailverification.officialvalidate.POST.uri", env.getProperty("api.emailverification.officialvalidate.POST.uri").build(); But I want spring to resolve automatically, as it does in actual run – Jigar Thakkar Jun 07 '17 at 20:13
  • can anybody help with this? – Jigar Thakkar Jun 13 '17 at 07:55

0 Answers0