Spring Boot - REST控制器,使用MockMvc进行测试,环境属性

By simon at 2018-02-28 • 0人收藏 • 145人看过

我在Spring引导应用程序中有一个REST控制器,很简单:

@RestController
@RequestMapping("/api")
public class MyRestController {
    @Autowired
    private Environment env;

    private String property1;

    @PostConstruct
    private void init() {
        this.property1 = env.getProperty("myproperties.property_1");
    }

    @GetMapping("/mydata")
    public String getMyData() {     
        System.out.println("property1: " + this.property1);
        ...
    }
在application.yml中,我定义了属性si类似于:
myproperties:
    property_1: value_1
当我使用REST控制器时,它按预期工作,值为value_1 阅读,并在GET方法公关ESENT。 现在我想用单元测试来测试它,也是类似的:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApp.class)
public class MyRestControllerTest {
    @Autowired
    private MappingJackson2HttpMessageConverter jacksonMessageConverter;    

    @Autowired
    private PageableHandlerMethodArgumentResolver pageableArgumentResolver; 

    @Autowired
    private ExceptionTranslator exceptionTranslator;    

    private MockMvc restMyRestControllerMockMvc;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);

        final MyRestController myRestController = new MyRestController();

        this.restMyRestControllerMockMvc = MockMvcBuilders.standaloneSetup(myRestController)
                .setCustomArgumentResolvers(pageableArgumentResolver).setControllerAdvice(exceptionTranslator)
                .setConversionService(createFormattingConversionService()).setMessageConverters(jacksonMessageConverter)
                .build();
    }

    @Test
    public void getMyDataTest() throws Exception {
        restMyRestControllerMockMvc.perform(get("/api/mydata"))
            .andExpect(status().isOk());
    }
当执行测试中的方法时,property property1是 空值。 这是为什么? 上面的代码部分由JHipster生成,我不确定这是否是 最佳的解决方案,只是重用它。 谢谢!

2 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

MockMvcBuilders.standaloneSetup不注入SpringContext所以属性数据 不可用,请将其更改为MockMvcBuilders。webAppContextSetup和 注入WebApplicationContext。 (最终你可以注入Environment bean到 MyRestController由它constructor,但在我看来这是Spring黑客。) 请同时添加@AutoConfigureMockMvc。 警告:还记得(在Maven布局项目)application.yml需要 被复制到src / test / resources。 代码示例:

@RestController
@RequestMapping("/api")
public class MyRestController {

    @Autowired
    private Environment env;

    private String envProperty;

    @Value("${myproperties.property_1}")
    private String valueProperty;

    @PostConstruct
    private void init() {
        this.envProperty = env.getProperty("myproperties.property_1");
    }

    @GetMapping("/mydata")
    public String getMyData() {
        System.out.println("envProperty: " + this.envProperty);
        System.out.println("valueProperty: " + this.valueProperty);
        return "";
    }

}

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApp.class)
@AutoConfigureMockMvc
public class MyRestControllerTest {

    @Autowired
    private MockMvc restMyRestControllerMockMvc;

    @Autowired
    private WebApplicationContext context;

    @Before
    public void setup() {
        final MyRestController myRestController = new MyRestController();
//        this.restMyRestControllerMockMvc = MockMvcBuilders.standaloneSetup(myRestController)
//                .build();
        this.restMyRestControllerMockMvc = MockMvcBuilders.webAppContextSetup(context)
                .build();
    }

    @Test
    public void getMyDataTest() throws Exception {
        restMyRestControllerMockMvc.perform(get("/api/mydata"));
    }
}

2018-02-28   #2

使用@Value注释来读取app.yml中的值

@RestController
@RequestMapping("/api")
public class MyRestController {
    @Autowired
    private Environment env;

    @Value("${myproperties.property_1}")
    private String property1;

    @GetMapping("/mydata")
    public String getMyData() {     
        System.out.println("property1: " + this.property1);
        ...
    }

登录后方可回帖

Loading...