java.lang.IllegalArgumentException:无法解析值"${crossOrigin}"中的占位符"crossOrigin"



我正在为控制器类编写测试用例。当我在像这样的每个方法之上添加跨源时@CrossOrigin(origins = "http://localhost:4200")那时测试用例正常运行,没有错误。

但我不想在每个方法之上硬编码跨源。为此,我在application.property文件中添加一个变量,并在类顶部的控制器中访问该变量。当我像这样添加跨源时,我能够从其他机器访问我的服务。但是我的测试用例失败了。

当我评论时,来自控制器类测试用例的这个@CrossOrigin(origins = "${crossOrigin}")运行正常。

谁能告诉我为什么会这样?

帐户控制器.java

package com.spacestudy.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.querydsl.core.Tuple;
import com.spacestudy.model.Account;
import com.spacestudy.model.*;
import com.spacestudy.services.*;
@RestController
@CrossOrigin(origins = "${crossOrigin}")
@RequestMapping("/api.spacestudy.com/spacestudy/rockefeller/admin/account")
public class AccountController {
@Autowired
AccountService accService;  

@GetMapping("/loadAcctLocationList")
public ResponseEntity<List<Account>> findLocation() {
return  ResponseEntity.ok(accService.findLocation());
}

应用程序属性

crossOrigin=http://localhost:4200
server.port=8088
spring.datasource.url=jdbc:postgresql://localhost/TestDB_002
spring.datasource.username=postgres
spring.datasource.password=postgres

测试帐户控制器

@RunWith(SpringRunner.class)
public class TestAccountController {
private MockMvc mockMvc;
@Mock
private AccountService accountService;
@InjectMocks
private AccountController accountController;
@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
}
@Test
public void findLocationTest() throws Exception {
Account account = new Account();
account.setsLocation("US");
account.setsLocation("pune");
List<Account> accountObj = new ArrayList<Account>();
accountObj.add(account);
Mockito.when(accountService.findLocation()).thenReturn(accountObj);
mockMvc.perform(get("/api.spacestudy.com/spacestudy/rockefeller/admin/account/loadAcctLocationList"))
.andExpect(status().isOk()).andExpect(jsonPath("$[0].sLocation", is("pune")));
}

堆栈跟踪

java.lang.IllegalArgumentException: Could not resolve placeholder 'crossOrigin' in value "${crossOrigin}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticStringValueResolver.resolveStringValue(StandaloneMockMvcBuilder.java:517)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.resolveCorsAnnotationValue(RequestMappingHandlerMapping.java:349)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.updateCorsConfig(RequestMappingHandlerMapping.java:319)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.initCorsConfiguration(RequestMappingHandlerMapping.java:303)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.initCorsConfiguration(RequestMappingHandlerMapping.java:55)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:558)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:264)
at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250)
at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping.registerHandlers(StandaloneMockMvcBuilder.java:490)
at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.registerMvcSingletons(StandaloneMockMvcBuilder.java:356)
at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.initWebAppContext(StandaloneMockMvcBuilder.java:341)
at org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder.build(AbstractMockMvcBuilder.java:139)
at com.spacestudy.controller.TestAccountController.setup(TestAccountController.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

我意识到这个问题已经有一年多了,但是我也遇到了这个问题。

解决方案是需要为 MockMvc 提供占位符值。

所以以下内容:

mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();

应该是 :

mockMvc = MockMvcBuilders.standaloneSetup(accountController).addPlaceholderValue("crossOrigin","https://some.url.com").build();

相关内容

  • 没有找到相关文章

最新更新