Skip to content

Commit

Permalink
Feat: User
Browse files Browse the repository at this point in the history
  • Loading branch information
currenjin committed Dec 17, 2024
1 parent 07c40ba commit 1a6821c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
27 changes: 27 additions & 0 deletions specification-pattern/src/main/java/com/currenjin/users/User.java
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;
}
}
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));
}
}

0 comments on commit 1a6821c

Please sign in to comment.