我正在尝试在 spring boot jpa 应用程序中为控制器类编写 Junit 测试用例。在 url 中的控制器类
InstituteIdentifier
变量中,我${InstituteIdentifier}
从application.property
文件中访问这样的变量。在这里,我在 url 中获得了该值
在测试用例中,我还使用注释InstituteIdentifier
从 application.property 访问变量。@value
我可以在控制台中打印该值。但是当我在测试用例 GET 方法 url 中访问该变量时,我收到了这个错误java.lang.IllegalArgumentException: Not enough variable values available to expand 'InstituteIdentifier
当我搜索这个错误时,我发现这里${InstituteIdentifier}
我们不需要给{}
. 当我删除{}
变量值时不在 url 中。
谁能告诉我该怎么做?
应用程序属性
InstituteIdentifier=vcufy2010
部门控制器
@RestController
@CrossOrigin(origins ="${crossOrigin}")
@RequestMapping("/spacestudy/${InstituteIdentifier}/control/searchfilter")
public class DepartmentController {
@Autowired
DepartmentService depService;
@GetMapping("/loadDepartments")
public ResponseEntity<Set<Department>> findDepName() {
Set<Department> depname = depService.findDepName();
return ResponseEntity.ok(depname);
}
}
测试部门控制器
@RunWith(SpringRunner.class)
@WebMvcTest(value=DepartmentController.class)
public class TestDepartmentController {
@Autowired
private MockMvc mockMvc;
@MockBean
DepartmentService departmentService;
@Value("${InstituteIdentifier}")
private String InstituteIdentifier;
@Test
public void testfindDepName() throws Exception {
System.out.println(InstituteIdentifier);//vcufy2010
Department department = new Department();
department.setsDeptName("ABC");
Set<Department> departmentObj = new HashSet<Department>();
departmentObj.add(department);
Mockito.when(departmentService.findDepName()).thenReturn(departmentObj);
mockMvc.perform(get("/spacestudy/${InstituteIdentifier}/control/searchfilter/loadDepartments")
.accept(MediaType.APPLICATION_JSON))