Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring Boot MVC测试-MockMvc始终为空_Java_Spring Boot_Junit - Fatal编程技术网

Java Spring Boot MVC测试-MockMvc始终为空

Java Spring Boot MVC测试-MockMvc始终为空,java,spring-boot,junit,Java,Spring Boot,Junit,我正试图编写我的第一个SpringMVC测试,但我无法让SpringBoot将MockMvc依赖项注入到我的测试类中。这是我的班级: @WebMvcTest public class WhyWontThisWorkTest { private static final String myUri = "uri"; private static final String jsonFileName = "myRequestBody.json"; @Autowired private

我正试图编写我的第一个SpringMVC测试,但我无法让SpringBoot将MockMvc依赖项注入到我的测试类中。这是我的班级:

@WebMvcTest
public class WhyWontThisWorkTest {

  private static final String myUri = "uri";
  private static final String jsonFileName = "myRequestBody.json";

  @Autowired
  private MockMvc mockMvc;

  @Test
  public void iMustBeMissingSomething() throws Exception {
    byte[] jsonFile = Files.readAllBytes(Paths.get("src/test/resources/" + jsonFileName));
    mockMvc.perform(
        MockMvcRequestBuilders.post(myUri)
            .content(jsonFile)
            .contentType(MediaType.APPLICATION_JSON))
        .andExpect(MockMvcResultMatchers.status().is2xxSuccessful());
  }
}

我已经使用IntelliJ的调试器进行了检查,可以确认mockMvc本身为空。因此,所有异常消息告诉我的都是“java.lang.NullPointerException”


我已经尝试过为测试类添加更多通用的Spring引导注释,如“@SpringBootTest”或“@RunWith(SpringRunner.class)”,以防它与初始化Spring有关,但没有运气。

奇怪,前提是您也尝试过
@RunWith(SpringRunner.class)
@SpringBootTest
。您是否也尝试过使用
@AutoConfigureMockMvc
注释?下面的示例运行良好

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void getHello() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(equalTo("Hello World of Spring Boot")));
    }
}
完整样本

<>也可以考虑下面的关于使用WebMVC测试和@ AutoCudioMoCKMVC注释的评论,详见

默认情况下,带有@WebMvcTest注释的测试还将自动配置Spring安全性和MockMvc(包括对HtmlUnit WebClient和Selenium WebDriver的支持)。为了更细粒度地控制MockMVC,可以使用@AutoConfigureMockMvc注释

通常,@WebMvcTest与@MockBean或@Import结合使用,以创建@Controller bean所需的任何协作者

如果要加载完整的应用程序配置并使用MoCKMVC,则应考虑@ SpRunBooTebug与@ AutoCudioMoCKMVC而不是此注释。 使用JUnit4时,此注释应与@RunWith(SpringRunner.class)结合使用


我也面临同样的问题。事实证明,
MockMvc
由于不兼容
@Test
注释而未被注入。导入是
org.junit.Test
,但将其更改为
org.junit.jupiter.api.Test
解决了这个问题。

接受的答案有效,但是我也没有导入
@RunWith(SpringRunner.class)

在我的例子中,我导入了
org.junit.Test
,而不是更新的
org.junit.jupiter.api.Test

如果您使用的是Maven,您可以避免犯此错误,将
junit vintage engine
排除在
spring boot starter test
依赖项之外:


org.springframework.boot
弹簧起动试验
测试
org.junit.vintage
朱尼特老式发动机

你能提供你的pom或gradle文件吗?@ElDuderino,很高兴能提供。快乐编码!非常感谢,这就成功了,让我通过了NullPointerException。我现在使用这些注释:@RunWith(SpringRunner.class)@SpringBootTest(classes=MyApplication.class)@AutoConfigureMockMvc@PropertySource(“classpath:application.properties”),不幸的是,测试现在卡在了Spring的徽标上。当我运行应用程序并手动发布文件时,它工作正常。我可能需要补充一点,这个测试基本上覆盖了应用程序的每一层,所以Spring Boot需要初始化几乎整个应用程序才能运行测试?您能否将json文件内容(以纯文本形式)放在字符串中,并将其用作内容而不是字节[]……如果它通过了测试,那么我们就知道它是否是一个格式问题。不是吗?我忘了声明我要在@ActiveProfiles中使用的配置文件(我有多个application.properties)。现在开始了。谢谢你的帮助,因为原来的问题现在已经解决了,所以我将你的答案标记为已接受。@ElDuderino perfect。很高兴一切都解决了。编码快乐,2020年新年快乐