Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Junit Test for exception and WebRequest

  1. #1
    Junior Member
    Join Date
    Dec 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Junit Test for exception and WebRequest

    I'm a beginner and i'm writing unittests and I've stumbled across something I can't find a solution for that fits my needs.

    I want to write some Junit Test for that exceptions.

    There is my class with my Method ($ == at, i don't know why but the editor doesn't accept at):

    $ControllerAdvice
    $RestController
    public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
     
        $ExceptionHandler(MethodArgumentTypeMismatchException.class)
        public final ResponseEntity<AccessError> numberFormatExceptionNotFoundException(
                    MethodArgumentTypeMismatchException ex, NumberFormatException exe, WebRequest request) {
                AccessError errorDetails = new AccessError();
                errorDetails.code("400");
                errorDetails.addErrorsItem(new Error("400",ex.getMessage()));
                errorDetails.setCode("400");
                errorDetails.setTimestamp(new Date().toInstant().atOffset(ZoneOffset.UTC));
                errorDetails.setMessage(HttpStatus.BAD_REQUEST.getReasonPhrase());
                errorDetails.setPath(((ServletWebRequest) request).getRequest().getRequestURI());
                return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
            }
        $Override
        protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex,
                HttpHeaders headers, HttpStatus status, WebRequest request) {
            AccessError errorDetails = new AccessError();
            errorDetails.code("400");
            errorDetails.addErrorsItem(new Error("400","Media Type Not Supported Exception"));
            errorDetails.setCode("400");
            errorDetails.setTimestamp(new Date().toInstant().atOffset(ZoneOffset.UTC));
            errorDetails.setMessage(HttpStatus.BAD_REQUEST.getReasonPhrase());
            errorDetails.setPath(((ServletWebRequest) request).getRequest().getRequestURI());
            return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
        }

    And there is my testClass :

     
    public class CustomizedResponseEntityExceptionHandlerTest {
    $Mock
    ResponseEntity<AccessError> responseEntity;
    WebRequest webRequest;
     
    $InjectMocks
    private CustomizedResponseEntityExceptionHandler custom = new CustomizedResponseEntityExceptionHandler();
     
    $Test
    public void numberFormatExceptionNotFoundExceptionTest() {
     
        WebRequest webRequest;
        String msg = "test";
        AccessError errors = new AccessError();
        errors.setPath("app");
        errors.getPath();
        errors.setTimestamp(new Date().toInstant().atOffset(ZoneOffset.UTC));
        errors.timestamp(new Date().toInstant().atOffset(ZoneOffset.UTC));
        ApiException apiException = new ApiException(errors, msg);
     
        ResponseEntity<AccessError> responseApi = custom.handleUserNotFoundException(apiException, webRequest.getHeaderNames());
        assertThatExceptionOfType(ApiException.class);
     
    }

    My Question is : How i can do a JUnit Test for that cases, which have webRequest and some exceptions ?

    I've tried a lot of thing but i think i don't have the right thinking method.

    Thanks !!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Junit Test for exception and WebRequest

    I don't know anything about JUnit testing. Try asking at this forum:
    https://coderanch.com/f/68/Testing

    Posted at: https://coderanch.com/t/748319/java/...ion-WebRequest
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JUnit Test Confusion.
    By Spencer4908 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2014, 02:05 AM
  2. Using Junit testing to test
    By egreenhorn in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 15th, 2014, 11:29 AM
  3. Using JUnit to test JSwing
    By Lerox in forum Java Theory & Questions
    Replies: 0
    Last Post: March 5th, 2014, 03:21 PM
  4. Exception in JUnit test (easy question)
    By opium in forum Exceptions
    Replies: 1
    Last Post: February 14th, 2012, 09:09 AM
  5. JUnit test for ER
    By raphytaffy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 09:26 PM

Tags for this Thread