情况简述:
服务能正常启动,跑单测时报错找不到 ‘server.context-path‘,找不到bean,应用上下文,等等。。。
原因:
在打日志的时候,开头有打印原因是因为没有环境变量,我觉得挺容易忽略的,至少我忽略了,一直在排查报错信息中提的bean为啥找不到…
No active profile set, falling back to default profiles: default
解决办法
在测试类入口增加注解:@ActiveProfiles(“dev”) 指定单测运行环境
异常截图
2024-04-19 14:43:09.920 WARN [program,,,] 36248 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loggingAspect': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'server.context-path' in value "${server.context-path}"
2024-04-19 14:43:09.946 ERROR [program,,,] 36248 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loggingAspect': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'server.context-path' in value "${server.context-path}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
启动日志开头
解决方法示例:
测试入口类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ProgramApplication.class)
@ActiveProfiles("dev")
public class ProgramApplicationTests {
protected MockMvc mockMvc;
@MockBean
protected VmsWorldcupService vmsWorldcupService;
@MockBean
protected UserInfosService userInfosRemote;
@Autowired
private Environment env;
@Before() //这个方法在每个方法执行之前都会执行一遍
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象
System.out.println( "context.listener.classes = " + env.getProperty("context.listener.classes"));
}
@Test
public void contextLoads() {
}
}
测试单元类
public class VideoxStaticCacheV1ControllerTest extends ProgramApplicationTests {
@Test
public void cdnDataVoTest2() throws Exception{
// 测试
mockMvc.perform(MockMvcRequestBuilders.get("/videox/staticcache//video/652444141/1"))
.andExpect(ProgramMockMvcResultMatchers.content().json("{\"code\":200,\"message\":\"OK\",\"body\":{\"name\":\"大冒险}}"));
}
}