实际上已解决,但是如果有人会遇到相同的问题,我将在这里留下解决方案。您必须手动配置占位符,例如:
java.lang.IllegalArgumentException: Could not resolve placeholder 'cors.origin.value' in value "${cors.origin.value}"
我正在尝试从控制器执行某种方法的简单单元测试,但是我遇到了一个问题:
public class TestApiUrlStrings {
public static final String API_V1_EVENTS = "api/v1/events";
public static final String API_V1_EVENTS_FIND_BY_GENRE = API_V1_EVENTS + "/Dance?page=0";
}
为什么会这样?这只是简单的单元测试,所以我不必为此设置整个上下文?
我的代码:
我正在调用的请求是API_V1_EVENTS_FIND_BY_GENRE:
public class EventControllerTest {
@Mock
EventService eventService;
@InjectMocks
EventController eventController;
MockMvc mockMvc;
public EventControllerTest() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(eventController)
.build();
}
@Test
public void findByGenre() throws Exception {
EventsDTO expected = EventsDTOdatasource.getEventsDTO();
when(eventService.findByGenre(anyString(),anyInt())).thenReturn(expected);
mockMvc.perform(get(TestApiUrlStrings.API_V1_EVENTS_FIND_BY_GENRE)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.events", hasSize(3)));
}
}
单元测试
@Slf4j
@CrossOrigin(value = "${cors.origin.value}")
@RestController
@RequestMapping(EventController.API_V1_EVENTS)
public class EventController {
public static final String API_V1_EVENTS = "api/v1/events";
private final EventService eventService;
public EventController(EventService eventService) {
this.eventService = eventService;
}
@GetMapping("/{musicGenre}")
@ResponseStatus(HttpStatus.OK)
public EventsDTO findByGenre(@PathVariable String musicGenre,
@RequestParam(value = "page", defaultValue = "0") Integer pageNum) {
return eventService.findByGenre(musicGenre, pageNum);
}
@PutMapping
@ResponseStatus(HttpStatus.CREATED)
public EventsDTO saveAll(@RequestBody EventsDTO eventsDTO) {
return this.eventService.saveAll(eventsDTO);
}
}
和控制器
ListView
为什么异常指向CORS值,而我在这里甚至不需要它? 如何解决呢?此类异常在任何地方都很少。