Skip to content
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

Added Fido #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/demo/NullPointerDereference.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ public int getNumberOfLegs(String animal) {
String trimmedAnimal = trim(animal);

int legs;
int arms;

if ("dog".equals(trimmedAnimal)) {
legs = 4;
} else if (trimmedAnimal.equals("kangaroo")) { // NullPointer issue raised
} else if ("kangaroo".equals(trimmedAnimal)) { // NullPointer issue raised
legs = 2;
} else if ("Centipede".equals(trimmedAnimal)) {
legs = 100;
} else if ("Fido".equals(trimmedAnimal)){
legs = 3;
} else {
throw new RuntimeException(String.format("Unknown Animal %s", trimmedAnimal));
throw new IllegalArgumentException(String.format("Unknown Animal %s", trimmedAnimal));
}

return legs;
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/demo/NullPointerDereferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ public class NullPointerDereferenceTest {

@Test
public void shouldGetDogPaws() {
assertEquals(underTest.getNumberOfLegs("dog"), 4);
assertEquals(4, underTest.getNumberOfLegs("dog"));
}

@Test(expected = NullPointerException.class)
@Test
public void shouldGetFidoPaws() {
assertEquals(3, underTest.getNumberOfLegs("Fido"));
}

@Test(expected = IllegalArgumentException.class)
public void ignoredTestThatWouldCoverBug() {
underTest.getNumberOfLegs("");
}
Expand Down