-
Notifications
You must be signed in to change notification settings - Fork 23
feat: add unit tests for State class including population updates and major cities #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
anthonydmays
merged 2 commits into
code-differently:main
from
nia-source:NiaP_Lesson_16
Apr 23, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...ts/objects_app/src/main/java/com/codedifferently/lesson16/states/PopulationException.java
This file contains hidden or 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,7 @@ | ||
package com.codedifferently.lesson16.states; | ||
|
||
class PopulationTooLowException extends Exception { | ||
public PopulationTooLowException(String message) { | ||
super(message); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
lesson_16/objects/objects_app/src/main/java/com/codedifferently/lesson16/states/State.java
This file contains hidden or 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,49 @@ | ||
package com.codedifferently.lesson16.states; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class State { | ||
private String name; | ||
private String capital; | ||
private int population; | ||
private double area; | ||
private Region region; | ||
private ArrayList<String> majorCities; | ||
|
||
enum Region { | ||
NORTHEAST, | ||
MIDWEST, | ||
SOUTH, | ||
WEST, | ||
PACIFIC | ||
} | ||
|
||
public State(String name, String capital, int population, double area, Region region) { | ||
this.name = name; | ||
this.capital = capital; | ||
this.population = population; | ||
this.area = area; | ||
this.region = region; | ||
this.majorCities = new ArrayList<>(); | ||
} | ||
|
||
public void updatePopulation(int newPopulation) throws PopulationTooLowException { | ||
if (newPopulation < 10000) { | ||
throw new PopulationTooLowException("Population too low for a state!"); | ||
} | ||
this.population = newPopulation; | ||
System.out.println("Population updated to " + newPopulation); | ||
} | ||
|
||
public void addMajorCity(String city) { | ||
majorCities.add(city); | ||
System.out.println(city = "added to major cities of " + name); | ||
} | ||
|
||
public void listMajorCities() { | ||
System.out.println("Major cities in " + name + ":"); | ||
for (String city : majorCities) { | ||
System.out.println("- " + city); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...n_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/states/StateTest.java
This file contains hidden or 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,67 @@ | ||
package com.codedifferently.lesson16.states; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class StateTest { | ||
|
||
private State state; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
state = new State("California", "Sacramento", 39500000, 423967, State.Region.WEST); | ||
} | ||
|
||
@Test | ||
public void constructor_ShouldInitializeFieldsCorrectly() { | ||
assertThat(state) | ||
.extracting("name", "capital", "population", "area", "region") | ||
.containsExactly("California", "Sacramento", 39500000, 423967.0, State.Region.WEST); | ||
} | ||
|
||
@Test | ||
public void updatePopulation_ShouldUpdateCorrectly_WhenValid() throws PopulationTooLowException { | ||
state.updatePopulation(5000000); | ||
assertThat(state).extracting("population").isEqualTo(5000000); | ||
} | ||
|
||
@Test | ||
public void updatePopulation_ShouldThrowException_WhenTooLow() { | ||
assertThatThrownBy(() -> state.updatePopulation(9000)) | ||
.isInstanceOf(PopulationTooLowException.class) | ||
.hasMessage("Population too low for a state!"); | ||
} | ||
|
||
@Test | ||
public void addMajorCity_ShouldAddCityToList() { | ||
state.addMajorCity("Los Angeles"); | ||
state.addMajorCity("San Francisco"); | ||
|
||
// Check if the cities were added (accessing via reflection for private field) | ||
assertThat(state) | ||
.extracting("majorCities") | ||
.asList() | ||
.containsExactly("Los Angeles", "San Francisco"); | ||
} | ||
|
||
@Test | ||
public void listMajorCities_ShouldPrintCityList() { | ||
state.addMajorCity("Los Angeles"); | ||
state.addMajorCity("San Diego"); | ||
|
||
// Capture the output | ||
ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(outContent)); | ||
|
||
state.listMajorCities(); | ||
|
||
System.setOut(System.out); // Reset back | ||
|
||
String expectedOutput = "Major cities in California:\n- Los Angeles\n- San Diego\n"; | ||
assertThat(outContent.toString().trim()).isEqualToIgnoringNewLines(expectedOutput.trim()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.