我的Spring Boot应用程序有
Controller
使用
Service
SharkMaster
)使用来自
properties
当我以常规模式运行应用程序时,它可以正常工作。
但是,当尝试为控制器类运行单元测试并为其提供模拟时
service
它试图创建真正的SharkMaster bean,但由于无法从配置中找到值而失败。
several posts
读一些
blog posts
关于类似的问题,但在目前阶段,应用程序正在崩溃
NullPointer
尝试在单元测试模式下运行时。
@Configuration
public class SharkMaster{
@Value("${sharkName}")
private String sharkName;
public SharkMaster(){
}
控制器测试类:
@RunWith(SpringRunner.class)
@WebMvcTest(SharkController.class)
@ContextConfiguration(classes = {SharkMaster.class})
@TestPropertySource("classpath:application-test.yml")
public class SharkControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private SharkService sharkService;
我试着加上
@PropertySource("classpath:application-test.yml")
我相信这个问题和
this one
,因为这里的情况是在单元测试阶段。