java.lang.IllegalArgumentException: Could not resolve placeholder ‘server.context-path‘ in value “${

情况简述:

服务能正常启动,跑单测时报错找不到 ‘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\":\"大冒险}}"));
				}
			}
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值