X Tutup
Skip to content

Commit d85c8ed

Browse files
committed
Implements the DataModelTestUsingRules exercise
1 parent 952e803 commit d85c8ed

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

com.vogella.android.test.juntexamples/app/src/main/java/com/vogella/android/test/juntexamples/model/TolkienCharacter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public class TolkienCharacter {
1616

1717
public TolkienCharacter(String name, int age, Race race) {
1818
super();
19+
if (age<0) {
20+
throw new IllegalArgumentException("Age is not allowed to be smaller than zero");
21+
}
1922
this.name = name;
2023
this.age = age;
2124
this.race = race;

com.vogella.android.test.juntexamples/app/src/test/java/com/vogella/android/test/juntexamples/DataModelTestUsingRules.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
import com.vogella.android.test.juntexamples.model.TolkienCharacter;
44

5-
import org.junit.Ignore;
65
import org.junit.Rule;
76
import org.junit.Test;
87
import org.junit.rules.ExpectedException;
98

10-
import java.util.List;
11-
129
import static com.vogella.android.test.juntexamples.model.Race.HOBBIT;
1310

1411

@@ -20,24 +17,18 @@ public class DataModelTestUsingRules {
2017
@Test
2118
public void testThatAgeMustBeLargerThanZeroViaSetter() {
2219
rule.expect(IllegalArgumentException.class);
20+
rule.expectMessage("Age is not allowed to be smaller than zero");
21+
2322
TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
24-
// use ExpectedException rule to check that the message is:
25-
// Age is not allowed to be smaller than zero
2623
frodo.setAge(-1);
2724

28-
// rule.expectMessage("Age is not allowed to be smaller than zero");
2925

3026
}
3127

3228
@Test
33-
@Ignore
3429
public void testThatAgeMustBeLargerThanZeroViaConstructor() {
3530
rule.expect(IllegalArgumentException.class);
31+
rule.expectMessage("Age is not allowed to be smaller than zero");
3632
TolkienCharacter frodo = new TolkienCharacter("Frodo", -1, HOBBIT);
37-
// use ExpectedException rule to check that the message is:
38-
// Age is not allowed to be smaller than zero
39-
40-
// rule.expectMessage("Age is not allowed to be smaller than zero");
41-
4233
}
4334
}

0 commit comments

Comments
 (0)
X Tutup