Spring StandaloneMockMvcBuilder无法解析被测控制器中的动态RequestMapping

时间:2017-06-07 20:11:29

标签: spring-mvc spring-junit

我们有带动态请求映射的控制器:

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

这些请求映射通过spring环境中的属性文件解析。

现在,在我们的SpringJUnit4ClassRunner测试类的设置方法中,我们正在尝试mockmvc:

MockMvcBuilders.standaloneSetup(controllerUnderTest).build()

但是MockMvcBuilder无法解析value = "${api.emailverification.officialvalidate.POST.uri}",尽管属性文件存在于spring环境中。 以下是实际代码:

控制器方法:

@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课程:

@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();
......
}

0 个答案:

没有答案