[URI-Creation] Difference Between URI.create() and new URI() (#12937)

* [URI-Creation] Difference Between URI.create() and new URI()

* [URI-Creation] split test methods for invalid & valid inputs
This commit is contained in:
Kai Yuan
2022-10-30 03:00:33 +01:00
committed by GitHub
parent dd8bcecd1b
commit 60bf069feb
3 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
package com.baeldung.uricreation;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.net.URISyntaxException;
import static org.junit.jupiter.api.Assertions.*;
public class UriCreationUnitTest {
@Test
void givenValidUriString_whenUsingConstructor_shouldGetExpectedResult() {
try {
URI myUri = new URI("https://www.baeldung.com/articles");
assertNotNull(myUri);
} catch (URISyntaxException e) {
fail();
}
}
@Test
void givenInvalidUriString_whenUsingConstructor_shouldGetExpectedResult() {
assertThrows(URISyntaxException.class, () -> new URI("I am an invalid URI string."));
}
@Test
void givenValidUriString_whenUsingCreateMethod_shouldGetExpectedResult() {
URI myUri = URI.create("https://www.baeldung.com/articles");
assertNotNull(myUri);
}
@Test
void givenInvalidUriString_whenUsingCreateMethod_shouldGetExpectedResult() {
assertThrows(IllegalArgumentException.class, () -> URI.create("I am an invalid URI string."));
}
}