Java 朱尼特不能';t在资源文件夹中加载该值

Java 朱尼特不能';t在资源文件夹中加载该值,java,junit-runner,Java,Junit Runner,我正在使用MockitoJUnit库测试我的控制器。有些变量声明为: @Value("${mine.port}") private Integer port; 当我运行测试时,它在资源文件夹中找不到作为属性文件的值mine.port。我的控制器运行没有问题,但是当我运行我的controller junit测试时,端口号是空的 我在课堂上使用了以下注释: @RunWith(MockitoJUnitRunner.class) @ContextConfiguration(classes = myCo

我正在使用MockitoJUnit库测试我的控制器。有些变量声明为:

@Value("${mine.port}")
private Integer port;
当我运行测试时,它在资源文件夹中找不到作为属性文件的值mine.port。我的控制器运行没有问题,但是当我运行我的controller 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容器:

@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(classes = myControl.class)
@WebAppConfiguration
@AutoConfigureMockMvc
要编写以控制器为中心的测试片,请不要使用Mockito运行程序:
@RunWith(MockitoJUnitRunner.class)
,否则无法启动Spring容器<代码>模拟JUnitRunner用于单元测试,而不是集成测试。
要知道何时以及如何使用SpringBoot编写单元和集成测试,您可以阅读以下内容

而不是将
@WebMvcTest
注释与Spring runner一起使用,例如:

import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@WebMvcTest
public class WebLayerTest {
   ...
}

你可以依靠这个来了解更多细节

spring上下文是否加载了该配置?是否尝试添加
@TestPropertySource
?如何运行测试?你使用构建自动化工具(maven,gradle…)吗?我也使用了springRunner,它仍然显示出同样的问题,你不必也使用springRunner,你只需要使用它。一个运行程序可以用来启动JUnit4测试。