【发布时间】:2019-02-04 17:29:56
【问题描述】:
我正在使用 MockitoJUnit 库来测试我的控制器。有一些变量被声明为:
@Value("${mine.port}")
private Integer port;
当我运行测试时,它在资源文件夹中找不到值 mine.port 作为属性文件。我的控制器运行没有问题,但是当我运行控制器 junit 测试时端口号为空。
我在课堂上使用了以下注释:
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(classes = myControl.class)
@WebAppConfiguration
@AutoConfigureMockMvc
我在 junit 测试中使用了以下方法
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders
.standaloneSetup(myController)
.build();
}
@Test
public void getName() throws Exception {
MockHttpServletRequestBuilder request =
MockMvcRequestBuilders.get("/service")
.contentType(MediaType.APPLICATION_JSON_VALUE);
MvcResult result = mockMvc.perform(request)
.andExpect(status().isOk()).andReturn(); }
我很困惑,有人知道我是否缺少某些设置吗?谢谢!
【问题讨论】:
-
spring 上下文是否正在加载该配置?
-
你试过加
@TestPropertySource吗? -
如何运行测试?你使用构建自动化工具(maven、gradle..)吗?
标签: java junit-runner