【问题标题】:RequestMapping placeholders don't work in JUnit testsRequestMapping 占位符在 JUnit 测试中不起作用
【发布时间】:2016-10-27 16:15:16
【问题描述】:

我有一个简单的 Spring MVC 控制器,它的 RequestMapping 是一个属性。这是一个包含控制器的罐子。下游应用会使用这个 jar 并使用公共端点,只有精确的 URL 可能因应用而异)

当我将我的 jar 包含到另一个应用程序中时,一切正常。该应用程序有一个属性或 yaml 文件,并且该属性已设置。我已验证端点工作正常。

但是,作为一名优秀的开发人员,我想做一个集成测试来验证由属性确定的 URL 是否正确公开。我可以在控制器中正确注入@Value,但@RequestMapping 中的${} 表达式不会被属性文件替换。我找到了几个线程(Spring Boot REST Controller Test with RequestMapping of Properties Value@RequestMapping with placeholder not working)但要么他们不适用,要么我尝试了他们所说的但我无法让它工作。

命中静态 (iWork) 端点的测试有效,但从属性中提取的测试 (iDontWork) 无效。

(这是 Spring 4.2.6)

控制器

@RestController
@RequestMapping(value = {"/${appName}", "/iWork"})
public class Controller {

    @Value("${appName}")
    private String appName;

    @RequestMapping(method= RequestMethod.GET)
    public String handlerMethod (HttpServletRequest request) throws Exception {
        // Proves the placeholder is injected in the class, but
        // Not in the RequestMapping
        assert appName != null;
        assert !appName.equals("${appName}");
        return "";
    }
}

控制器测试

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigWebContextLoader.class,
    classes = { ControllerTest.Config.class })
public class ControllerTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mvc;

    @Before
    public void setup() {
        mvc = MockMvcBuilders
            .webAppContextSetup(context)
            .build();
    }

    @Configuration
    @ComponentScan(basePackages = {"test"})
    static class Config {
        // because @PropertySource doesnt work in annotation only land
        @Bean
        PropertyPlaceholderConfigurer propConfig() {
            PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
            ppc.setLocation(new ClassPathResource("test.properties"));
            return ppc;
        }
    }

    @Test
    public void testStaticEndpoint() throws Exception {
        mvc.perform(get("/iWork")).andExpect(status().isOk());
    }

    @Test
    public void testDynamicEndpoint() throws Exception {
        mvc.perform(get("/iDontWork")).andExpect(status().isOk());
    }
}

test.properties

appName = iDontWork

【问题讨论】:

    标签: java spring spring-mvc junit


    【解决方案1】:

    你“只是”失踪了

    @EnableWebMvc
    

    在您的@Configuration 课程上。没有它,Spring 的 Mock MVC 堆栈将使用 DefaultAnnotationHandlerMapping 注册您的控制器处理程序方法,这不够聪明,无法解析 @RequestMapping 中的占位符。

    如果您确实提供它,Mock MVC 将改为使用RequestMappingHandlerMapping,即。

    【讨论】:

      【解决方案2】:

      您需要在创建 mockMvc 时添加占位符值,如下所示。

      @Before
      public void setup() {
          MockitoAnnotations.initMocks(this);
          mockMvc = MockMvcBuilders.standaloneSetup(accountController)
                      .addPlaceholderValue("propertyName", "propertyValue")
                      .build();
      }
      

      【讨论】:

        猜你喜欢
        • 2014-06-17
        • 2011-07-28
        • 2020-10-10
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 2018-02-19
        • 2013-10-19
        • 2017-04-23
        相关资源
        最近更新 更多