-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
specification-pattern/src/main/java/com/currenjin/users/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.currenjin.users; | ||
|
||
public class User { | ||
private final String name; | ||
private final int age; | ||
|
||
public User(String name, int age) { | ||
if (age < 0) { | ||
throw new IllegalArgumentException("Age must be a positive integer"); | ||
} | ||
|
||
if (name == null || name.isBlank()) { | ||
throw new IllegalArgumentException("Username cannot be null or empty"); | ||
} | ||
|
||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
specification-pattern/src/test/java/com/currenjin/users/UserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.currenjin.users; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class UserTest { | ||
|
||
public static final String NAME = "currenjin"; | ||
public static final int AGE = 24; | ||
|
||
@Test | ||
void field_test() { | ||
User user = new User(NAME, AGE); | ||
|
||
assertEquals(NAME, user.getName()); | ||
assertEquals(AGE, user.getAge()); | ||
} | ||
|
||
@Test | ||
void age_cannot_be_less_than_zero() { | ||
assertThrows(IllegalArgumentException.class, () -> new User(NAME, -1)); | ||
} | ||
|
||
@Test | ||
void name_cannot_be_null_or_blank() { | ||
assertThrows(IllegalArgumentException.class, () -> new User("", AGE)); | ||
assertThrows(IllegalArgumentException.class, () -> new User(" ", AGE)); | ||
assertThrows(IllegalArgumentException.class, () -> new User(null, AGE)); | ||
} | ||
} |