[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 69ff3c0c3b
commit 146ffa71d0
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-networking-4</artifactId>
<name>core-java-networking-4</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<finalName>core-java-networking-4</finalName>
</build>
</project>

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."));
}
}

View File

@ -98,6 +98,7 @@
<module>core-java-lang-syntax-2</module>
<module>core-java-networking</module>
<module>core-java-networking-2</module>
<module>core-java-networking-4</module>
<module>core-java-nio</module>
<module>core-java-nio-2</module>
<module>core-java-numbers</module>