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

时间:2018-05-30 14:13:38

标签: spring-data-jpa global-variables junit4

我正在编写控制器类的测试用例。当我在每个方法之上添加交叉原点时@CrossOrigin(origins = "http://localhost:4200") 那时测试用例正常运行而没有错误。

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

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

任何人都可以告诉我它为什么会发生吗?

AccountController.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());
    }

Application.property

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

TestAccountController

    @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)

1 个答案:

答案 0 :(得分:2)

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

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

以下内容:

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

应该是:

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