0

I am trying to get user from database using spring boot REST controller. I am passing the id as a request parameter. If i pass a value 0004 it treats it as 4 and the tests pass. However if I pass a value as 0030 it treats as 24.

ControllerTest.java

@Test
public void get_user_by_id_should_return_user_with_id_0030() throws Exception {
    User user = new User(0030, "sample@example.com", LocalDate.parse("2018-12-27"), "Kitchen Assistant", Role.user);
    when(userServiceMock.searchByemployeeId(0030)).thenReturn(user);

    MockHttpServletRequestBuilder getUserByEmployeeId = get("/admin/user?employeeId=0030");
    getUserByEmployeeId.header("Authorization", "Bearer " + adminJwtToken);
    JSONObject expectedResponse = new JSONObject();
    expectedResponse.put("employeeId", 0030);
    expectedResponse.put("companyEmailId", "sample@example.com");
    expectedResponse.put("dateOfJoining", "2018-12-27");
    expectedResponse.put("designation", "Kitchen Assistant");
    expectedResponse.put("role", "user");

    mockMvc.perform(getUserByEmployeeId).andExpect(status().isOk());
    verify(userServiceMock, times(1)).searchByemployeeId(0030);
}

Controller.java

  @GetMapping(path = "/admin/user",params = "employeeId")
    public ResponseEntity<User> getUserWithEmploeeId(@RequestParam Integer employeeId) {
        User user = userService.searchByemployeeId(employeeId);
        return new ResponseEntity<>(user, OK);
    }

However, passing 0030 as a parameter does not treat as 30. It treats 30 as 24, I do not understand why.

Error:

Actual invocations have different arguments:
userService bean.searchByemployeeId(30);
-> at com.example.controller.AdminController.getUserWithEmploeeId(AdminController.java:85)

Comparison Failure: 
<Click to see difference>

Argument(s) are different! Wanted:
userService bean.searchByemployeeId(24);
-> at com.example.controller.controller.AdminControllerTest.get_user_by_id_should_return_user_with_id_0030(AdminControllerTest.java:1595)
Actual invocations have different arguments:
userService bean.searchByemployeeId(30);
-> at com.example.controller.controller.AdminController.getUserWithEmploeeId(AdminController.java:85)
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
wick3d
  • 1,164
  • 14
  • 41
  • 6
    The question title does imply that you do understand that these numbers are treated as octal numbers. Octal numbers in java are marked by having leading zeroes. So if you want 10 interpreted as decimal you write `int x = 10`, if you want it interpreted as octal you write `int x = 010`. That's just the java syntax. But this leaves the question: Why do you feel the need to have leading zeroes in your numbers anyway? Why not just use `30` when you want to use the decimal number 30? – OH GOD SPIDERS Dec 17 '21 at 18:25
  • Employees id have leading zeroes `0030`. – wick3d Dec 17 '21 at 18:43
  • 4
    There is a difference between the value of number and its representation. If you need to represent the employee ids with leading zeroes [then you should to so when displaying the data](https://stackoverflow.com/questions/473282/how-can-i-pad-an-integer-with-zeros-on-the-left). But if you want your ids to have the decimal value of 30 then the only way to do that in java is by writing `30`. There is no way for you to write `0030` in java code and not have that interpreted as an octal number. – OH GOD SPIDERS Dec 17 '21 at 18:47
  • You may want to check [Why is JSON invalid if an integer begins with a leading zero](https://stackoverflow.com/questions/27361565/why-is-json-invalid-if-an-integer-begins-with-a-leading-zero). Regarding statement: _Employees id have leading zeroes `0030`_ -- then employee Id is string – Nowhere Man Dec 17 '21 at 22:28

0 Answers0