gpt4 book ai didi

java - Spring Boot - REST Controller ,使用 MockMvc 进行测试,环境属性

转载 作者:行者123 更新时间:2023-12-02 02:17:58 33 4
gpt4 key购买 nike

我在 Spring boot 应用程序中有一个 REST Controller ,简单地说:

@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 中,我定义了类似于以下内容的属性:

myproperties:
property_1: value_1

当我使用 REST Controller 时,它按预期工作,读取值 value_1,并出现在 GET 方法中。

现在我想用单元测试来测试它,也类似:

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

执行test中的方法时,属性property1的值为null。

这是为什么?

上面的代码部分是由JHipster生成的,我不确定这是否是最佳解决方案,只是重用它。

谢谢!

最佳答案

MockMvcBuilders.standaloneSetup not loads SpringContext so properties data are not available. You can verify this by using @Value("${myproperties.property_1}") annotation directly inside MyRestControllerTest - it will return "value_1" value (but inside MyRestController - will return null).

请将其更改为 MockMvcBuilders.webAppContextSetup 并注入(inject) WebApplicationContext。 (最终你可以通过它的构造函数将环境bean注入(inject)到MyRestController中,但在我看来这是Spring hacking。)

警告:还要记住(在 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 "";
}

@GetMapping("/myproblem")
public String getMyProblem() {
throw new IllegalArgumentException();
}

}

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

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"));
}

@Test
public void getMyProblemTest() throws Exception {
restMyRestControllerMockMvc.perform(get("/api/myproblem"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isConflict());
}

}

@ControllerAdvice
public class ControllerAdvicer {

@ResponseStatus(HttpStatus.CONFLICT)
@ExceptionHandler(IllegalArgumentException.class)
public String assertionException(final IllegalArgumentException e) {
return "xxx";
}

}

关于java - Spring Boot - REST Controller ,使用 MockMvc 进行测试,环境属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48996919/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com