执行 JUnit 测试时 ServletContext 无法打开属性文件
ServletContext cannot open properties file when executing JUnit test
我正在使用 JUnit 4 和 MockMvc 测试 REST 控制器。当我几周前编写测试时,一切都按预期进行。我对代码做了一些修改,但没有更改 JUnit 测试。现在,当我尝试 运行 我的测试时,出现错误:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/application.properties]
这是我的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyServerApplication.class)
@SpringBootTest
@Transactional
public class MovieControllerTest {
private MockMvc mockMvc;
@Autowired
private MovieRepository movieRepository;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
// Some tests
}
我的主要 class:
@SpringBootApplication
public class MyServerApplication{
public static void main(String[] args) {
SpringApplication.run(MyServerApplication.class, args);
}
}
我的 application.properties
文件位于 src/main/resources
。我没有移动这个文件,我什么也没做,只是在我的服务中添加了一些代码并在我的文件中添加了一些属性。
我阅读了 SO 问题和文档,并尝试了这些解决方案:
- 检查
src/main/resources
是否仍在我的测试中 classpath
- 在我的测试注释下添加
@PropertySource("classpath:application.properties")
;它没有用,所以我尝试创建一个 src/test/resources
,里面有一个 application.properties
的副本,正如一个 post 中所建议的
- 在主class中添加
@PropertySource("classpath:application.properties")
而不是测试class
- 添加
@WebAppConfiguration
注释
- 添加
@WebMvcTest
注释
我当然没有同时尝试所有这些解决方案,我在每次失败后删除了添加的代码。
我仍然可以 运行 我的代码没有任何问题,只有测试 class 结果 FileNotFoundException
.
如何解决这个问题?为什么我的测试 class 有问题,但当我 运行 我的服务器时一切正常?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyServerApplication.class)
@SpringBootTest
@Transactional
public class MovieControllerTest { ... }
这就是您的测试 class。使用 @SpringBootTest
时不应使用 @ContextConfiguration
(请参阅 Spring 引导参考指南的 testing chapter)。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@Transactional
public class MovieControllerTest { ... }
我还建议您使用 Spring Boot 进行测试,而不是尝试手动执行操作。对于模拟 mvc 测试 Spring 引导应用程序,已经为您完成了特殊的切片和设置。
要启用此功能,请将 @AutoConfigureMockMvc
添加到您的测试并将 @Autowired
放在 MockMvc
字段中(并删除 @Before
方法中的设置)。
我正在使用 JUnit 4 和 MockMvc 测试 REST 控制器。当我几周前编写测试时,一切都按预期进行。我对代码做了一些修改,但没有更改 JUnit 测试。现在,当我尝试 运行 我的测试时,出现错误:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/application.properties]
这是我的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyServerApplication.class)
@SpringBootTest
@Transactional
public class MovieControllerTest {
private MockMvc mockMvc;
@Autowired
private MovieRepository movieRepository;
@Autowired
private WebApplicationContext wac;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
// Some tests
}
我的主要 class:
@SpringBootApplication
public class MyServerApplication{
public static void main(String[] args) {
SpringApplication.run(MyServerApplication.class, args);
}
}
我的 application.properties
文件位于 src/main/resources
。我没有移动这个文件,我什么也没做,只是在我的服务中添加了一些代码并在我的文件中添加了一些属性。
我阅读了 SO 问题和文档,并尝试了这些解决方案:
- 检查
src/main/resources
是否仍在我的测试中 classpath - 在我的测试注释下添加
@PropertySource("classpath:application.properties")
;它没有用,所以我尝试创建一个src/test/resources
,里面有一个application.properties
的副本,正如一个 post 中所建议的
- 在主class中添加
@PropertySource("classpath:application.properties")
而不是测试class - 添加
@WebAppConfiguration
注释 - 添加
@WebMvcTest
注释
我当然没有同时尝试所有这些解决方案,我在每次失败后删除了添加的代码。
我仍然可以 运行 我的代码没有任何问题,只有测试 class 结果 FileNotFoundException
.
如何解决这个问题?为什么我的测试 class 有问题,但当我 运行 我的服务器时一切正常?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyServerApplication.class)
@SpringBootTest
@Transactional
public class MovieControllerTest { ... }
这就是您的测试 class。使用 @SpringBootTest
时不应使用 @ContextConfiguration
(请参阅 Spring 引导参考指南的 testing chapter)。
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@Transactional
public class MovieControllerTest { ... }
我还建议您使用 Spring Boot 进行测试,而不是尝试手动执行操作。对于模拟 mvc 测试 Spring 引导应用程序,已经为您完成了特殊的切片和设置。
要启用此功能,请将 @AutoConfigureMockMvc
添加到您的测试并将 @Autowired
放在 MockMvc
字段中(并删除 @Before
方法中的设置)。