|
| 1 | +package com.twilio.exception; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | +import static org.junit.Assert.assertSame; |
| 7 | + |
| 8 | +@SuppressWarnings("ThrowableInstanceNeverThrown") |
| 9 | +public class ApiExceptionTest { |
| 10 | + |
| 11 | + private final String anyMessage = "message for test"; |
| 12 | + private final Throwable anyCause = new RuntimeException("some root cause"); |
| 13 | + private final String anyMoreInfo = "more info"; |
| 14 | + private final int anyErrorCode = 123; |
| 15 | + private final int anyHttpStatus = 200; |
| 16 | + |
| 17 | + |
| 18 | + @Test |
| 19 | + public void singleArgConstructorShouldPreserveMessage() { |
| 20 | + ApiException error = new ApiException(anyMessage); |
| 21 | + assertEquals(anyMessage, error.getMessage()); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void twoArgConstructorShouldPreserveMessageAndCause() { |
| 26 | + ApiException error = new ApiException(anyMessage, anyCause); |
| 27 | + assertEquals("Message", anyMessage, error.getMessage()); |
| 28 | + assertSame("Cause", anyCause, error.getCause()); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void fullConstructorShouldPreserveAllValues() { |
| 33 | + ApiException error = new ApiException(anyMessage, anyErrorCode, anyMoreInfo, anyHttpStatus, anyCause); |
| 34 | + assertEquals("Message", anyMessage, error.getMessage()); |
| 35 | + assertSame("Cause", anyCause, error.getCause()); |
| 36 | + assertEquals("More info", anyMoreInfo, error.getMoreInfo()); |
| 37 | + assertEquals("Error code", anyErrorCode, error.getCode().intValue()); |
| 38 | + assertEquals("Status code", anyHttpStatus, error.getStatusCode().intValue()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void getCodeShouldNotThrowExceptionWhenCodeIsNull() { |
| 43 | + ApiException error = new ApiException(anyMessage); |
| 44 | + assertEquals(null, error.getCode()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void getStatusCodeShouldNotThrowExceptionWhenCodeIsNull() { |
| 49 | + ApiException error = new ApiException(anyMessage); |
| 50 | + assertEquals(null, error.getStatusCode()); |
| 51 | + } |
| 52 | + |
| 53 | +} |
0 commit comments