Skip to content

Commit e2b10ba

Browse files
authored
Update tests bird watcher (#3077)
* Update tests bird watcher * Add itIncrementDoesNotChangeCountForOtherDays test Added a test to verify that incrementTodaysCount does not affect counts for other days. * Fix indentation * Refactor BirdWatcherTest to use new array for each test * Change getLastWeek to static method * Change getLastWeek to static method * Fix class name from BirdWatched to BirdWatcher * Fix assertion in getCountForFirstDays test * Shift new test to the end of task 5 * Fix test for incrementTodaysCount method Update test to check correct increment behavior for today's count. * Add contributor to config.json
1 parent c020ed7 commit e2b10ba

4 files changed

Lines changed: 46 additions & 33 deletions

File tree

exercises/concept/bird-watcher/.meta/config.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"samuelteixeiras",
44
"ystromm"
55
],
6+
"contributors": [
7+
"jagdish-15"
8+
],
69
"files": {
710
"solution": [
811
"src/main/java/BirdWatcher.java"

exercises/concept/bird-watcher/.meta/src/reference/java/BirdWatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public BirdWatcher(int[] birdsPerDay) {
66
this.birdsPerDay = birdsPerDay.clone();
77
}
88

9-
public int[] getLastWeek() {
9+
public static int[] getLastWeek() {
1010
return new int[] { 0, 2, 5, 3, 7, 8, 4 };
1111
}
1212

exercises/concept/bird-watcher/src/main/java/BirdWatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public BirdWatcher(int[] birdsPerDay) {
66
this.birdsPerDay = birdsPerDay.clone();
77
}
88

9-
public int[] getLastWeek() {
9+
public static int[] getLastWeek() {
1010
throw new UnsupportedOperationException("Please implement the BirdWatcher.getLastWeek() method");
1111
}
1212

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,112 @@
11
import org.junit.jupiter.api.DisplayName;
22
import org.junit.jupiter.api.Tag;
33
import org.junit.jupiter.api.Test;
4-
import org.junit.jupiter.api.BeforeEach;
54

6-
import static org.assertj.core.api.Assertions.*;
5+
import static org.assertj.core.api.Assertions.assertThat;
76

87
public class BirdWatcherTest {
98

10-
private static final int DAY1 = 0;
11-
private static final int DAY2 = 2;
12-
private static final int DAY3 = 5;
13-
private static final int DAY4 = 3;
14-
private static final int DAY5 = 7;
15-
private static final int DAY6 = 8;
16-
private static final int TODAY = 4;
17-
18-
private BirdWatcher birdWatcher;
19-
private final int[] lastWeek = {DAY1, DAY2, DAY3, DAY4, DAY5, DAY6, TODAY};
20-
21-
@BeforeEach
22-
public void setUp() {
23-
birdWatcher = new BirdWatcher(lastWeek);
24-
}
25-
269
@Test
2710
@Tag("task:1")
2811
@DisplayName("The getLastWeek method correctly returns last week's counts")
2912
public void itTestGetLastWeek() {
30-
assertThat(birdWatcher.getLastWeek())
31-
.containsExactly(DAY1, DAY2, DAY3, DAY4, DAY5, DAY6, TODAY);
13+
assertThat(BirdWatcher.getLastWeek()).isEqualTo(new int[] {0, 2, 5, 3, 7, 8, 4});
3214
}
3315

3416
@Test
3517
@Tag("task:2")
3618
@DisplayName("The getToday method correctly returns today's counts")
3719
public void itTestGetToday() {
38-
assertThat(birdWatcher.getToday()).isEqualTo(TODAY);
20+
int[] counts = new int[] {8, 8, 9, 5, 4, 7, 10};
21+
BirdWatcher birdWatcher = new BirdWatcher(counts);
22+
assertThat(birdWatcher.getToday()).isEqualTo(10);
3923
}
4024

4125
@Test
4226
@Tag("task:3")
4327
@DisplayName("The incrementTodaysCount method correctly increments today's counts")
4428
public void itIncrementTodaysCount() {
29+
int[] counts = new int[] {8, 8, 9, 2, 1, 6, 4};
30+
BirdWatcher birdWatcher = new BirdWatcher(counts);
4531
birdWatcher.incrementTodaysCount();
46-
assertThat(birdWatcher.getToday()).isEqualTo(TODAY + 1);
32+
assertThat(birdWatcher.getToday()).isEqualTo(5);
4733
}
4834

4935
@Test
5036
@Tag("task:4")
51-
@DisplayName("The hasDayWithoutBirds method returns true when day had no visits")
37+
@DisplayName("The hasDayWithoutBirds method returns true when at least one day had no visits")
5238
public void itHasDayWithoutBirds() {
39+
int[] counts = new int[] {5, 5, 4, 0, 7, 6, 7};
40+
BirdWatcher birdWatcher = new BirdWatcher(counts);
5341
assertThat(birdWatcher.hasDayWithoutBirds()).isTrue();
5442
}
5543

5644
@Test
5745
@Tag("task:4")
5846
@DisplayName("The hasDayWithoutBirds method returns false when no day had zero visits")
5947
public void itShouldNotHaveDaysWithoutBirds() {
60-
birdWatcher = new BirdWatcher(new int[]{1, 2, 5, 3, 7, 8, 4});
48+
int[] counts = new int[] {4, 5, 9, 10, 9, 4, 3};
49+
BirdWatcher birdWatcher = new BirdWatcher(counts);
6150
assertThat(birdWatcher.hasDayWithoutBirds()).isFalse();
6251
}
6352

6453
@Test
6554
@Tag("task:4")
6655
@DisplayName("The hasDayWithoutBirds method returns true if the last day has zero visits")
6756
public void itHasLastDayWithoutBirds() {
68-
birdWatcher = new BirdWatcher(new int[]{1, 2, 5, 3, 7, 8, 0});
57+
int[] counts = new int[] {1, 2, 5, 3, 7, 8, 0};
58+
BirdWatcher birdWatcher = new BirdWatcher(counts);
6959
assertThat(birdWatcher.hasDayWithoutBirds()).isTrue();
7060
}
7161

7262
@Test
7363
@Tag("task:5")
7464
@DisplayName("The getCountForFirstDays method returns correct visits' count for given number of days")
7565
public void itTestGetCountForFirstDays() {
76-
assertThat(birdWatcher.getCountForFirstDays(4)).isEqualTo(DAY1 + DAY2 + DAY3 + DAY4);
66+
int[] counts = new int[] {5, 9, 12, 6, 8, 8, 17};
67+
BirdWatcher birdWatcher = new BirdWatcher(counts);
68+
assertThat(birdWatcher.getCountForFirstDays(4)).isEqualTo(32);
7769
}
7870

7971
@Test
8072
@Tag("task:5")
8173
@DisplayName("The getCountForFirstDays method returns overall count when number of days is higher than array size")
8274
public void itTestGetCountForMoreDaysThanTheArraySize() {
83-
assertThat(birdWatcher.getCountForFirstDays(10))
84-
.isEqualTo(DAY1 + DAY2 + DAY3 + DAY4 + DAY5 + DAY6 + TODAY);
75+
int[] counts = new int[] {5, 9, 12, 6, 8, 8, 17};
76+
BirdWatcher birdWatcher = new BirdWatcher(counts);
77+
assertThat(birdWatcher.getCountForFirstDays(10)).isEqualTo(65);
78+
}
79+
80+
@Test
81+
@Tag("task:5")
82+
@DisplayName("The incrementTodaysCount method adds one to getCountForFirstDays method")
83+
public void itIncrementDoesNotChangeCountForOtherDays() {
84+
int[] counts = new int[] {5, 1, 0, 4, 2, 3, 0};
85+
BirdWatcher birdWatcher = new BirdWatcher(counts);
86+
87+
int countPriorIncrement = birdWatcher.getCountForFirstDays(7);
88+
birdWatcher.incrementTodaysCount();
89+
int countAfterIncrement = birdWatcher.getCountForFirstDays(7);
90+
91+
assertThat(countPriorIncrement).isEqualTo(15);
92+
assertThat(countAfterIncrement).isEqualTo(16);
8593
}
8694

8795
@Test
8896
@Tag("task:6")
8997
@DisplayName("The getBusyDays method returns the correct count of busy days")
9098
public void itTestGetCountForBusyDays() {
91-
// DAY3, DAY5 and DAY6 are all >= 5 birds
92-
assertThat(birdWatcher.getBusyDays()).isEqualTo(3);
99+
int[] counts = new int[] {4, 9, 5, 7, 8, 8, 2};
100+
BirdWatcher birdWatcher = new BirdWatcher(counts);
101+
assertThat(birdWatcher.getBusyDays()).isEqualTo(5);
93102
}
94103

95104
@Test
96105
@Tag("task:6")
97106
@DisplayName("The getBusyDays method correctly returns zero in case of no busy days")
98107
public void itShouldNotHaveBusyDays() {
99-
birdWatcher = new BirdWatcher(new int[]{1, 2, 3, 3, 2, 1, 4});
108+
int[] counts = new int[] {1, 2, 3, 3, 2, 1, 4};
109+
BirdWatcher birdWatcher = new BirdWatcher(counts);
100110
assertThat(birdWatcher.getBusyDays()).isEqualTo(0);
101111
}
102112
}

0 commit comments

Comments
 (0)