Search code examples
junitspring-data-jpaapplication.properties

How to access variable value in test case HTTP method URL


I am trying to write Junit test cases for controller classes In spring boot jpa application. In controller class InstituteIdentifier variable in url I am accessing like this ${InstituteIdentifier} from application.property file. Here I am getting that value in url

In test case also I am accessing InstituteIdentifier variable from application.property using @value annotation. I am able to print that value in console. But when I am accessing that variable in test case GET method url I am getting this error java.lang.IllegalArgumentException: Not enough variable values available to expand 'InstituteIdentifier

When I search for this error I found that here ${InstituteIdentifier} we don't need to give {}. when I am removing {} variable value is not feaching in url.

Can any one tell me how to do that?

application.property

InstituteIdentifier=vcufy2010

DepartmenController

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

TestDepartmentController

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

Solution

  • When I search for this error I found that here ${InstituteIdentifier} we don't need to give {}. when I am removing {} variable value is not feaching in url.

    To use the value of a String variable, you don't need {} nor $.
    In fact you don't need to evaluate anything. It was already did thanks to @Value by Spring.

    So in your test, that is correct :

    @Value("${InstituteIdentifier}")
    private String instituteIdentifier;
    

    as you need to retrieve the value from the loaded properties.

    And then you just need to pass the value of the instituteIdentifier variable in the submitted url by concatenating Strings :

    mockMvc.perform(get("/spacestudy/" + instituteIdentifier +  "/control/searchfilter/loadDepartments")
                                .accept(MediaType.APPLICATION_JSON))