0

I want to write a JUnit test (Java unit) for my UserController class but I don't know how can I do it.

UserController:

@RestController
@RequestMapping(CompositeController.ENTRY)
public class UserController {
protected final static String ENTRY = "/demo/v1/composite";
private UserService userService;

@Autowired
public UserController(UserService userService) {
    this.userService = userService;
}


 @GetMapping(path = "/isadmin")
 public ResponseEntity<Boolean> checkadmin(@RequestHeader String nickname){

 return userService.checkifadmin(nickname);

 }

UserService:

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public interface UserService {
ResponseEntity<Boolean> checkifadmin(String nickname);
}

UserServiceImpl:

public class UserServiceImpl implements UserService {

 private final String userBaseAdress = "http://localhost:7777";
 private final String userBasePath = "/demo/v1/user";

 public ResponseEntity<Boolean> checkifadmin(String nickname) {          
        HttpHeaders headers = new HttpHeaders();            
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("nickname", nickname);       
        HttpEntity<String> entity = new HttpEntity<String>(headers);             
        RestTemplate restTemplate = new RestTemplate();          
        ResponseEntity<Boolean> response = restTemplate.exchange(userBaseAdress + userBasePath + "/isadmin",
                HttpMethod.GET, entity, Boolean.class);         
        return response;
    }
}
The Bitman
  • 1,279
  • 1
  • 11
  • 25
Dja111
  • 43
  • 4
  • 1
    Welcome to StackOverFlow, for my knowledge we don't perform unit test on controllers, unit test like their name, are used to test a 'unit' a simple functionality example adding to database or update, for controllers we perform 'integration' tests – Elarbi Mohamed Aymen Sep 30 '18 at 15:17
  • 1
    @ElarbiMohamedAymen, performing unit test on controllers is perfectly ok as long as you focus on specific units of it. Unit testing controller levels is really cool for conditional status code responses that are only determined on controller functions. Mocking is a good place to start. Check out S.K.'s answer. – Allan Chua Oct 01 '18 at 06:52
  • To make it clear, @Dja111's question is really valid. Controller unit test should focus on controller level and not lower levels of the dependency graph. – Allan Chua Oct 01 '18 at 15:35
  • There are two level of unit testing. Testing with MockMvc and call the controller methods through the framework or call the controller methods direct in the tests and simulate the framework responses by preset mocks. The first one is on half the way to intergration testing, the second one is the classical unit testing. – The Bitman Oct 05 '18 at 08:18
  • [It is impossible to answer your question because you do not provide a specification of what your code ought to do](https://stackoverflow.com/a/53757321/545127). – Raedwald Dec 13 '18 at 09:01

1 Answers1

5

You can use Spring's MockMvc. Check the details at : https://spring.io/guides/gs/testing-web/

Another useful approach is to not start the server at all, but test only the layer below that, where Spring handles the incoming HTTP request and hands it off to your controller. That way, almost the full stack is used, and your code will be called exactly the same way as if it was processing a real HTTP request, but without the cost of starting the server. To do that we will use Spring’s MockMvc, and we can ask for that to be injected for us by using the @AutoConfigureMockMvc annotation on the test case:

S.K.
  • 3,597
  • 2
  • 16
  • 31