Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb90be1

Browse files
committed
Sample code
1 parent 956e920 commit bb90be1

File tree

7 files changed

+150
-32
lines changed

7 files changed

+150
-32
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.serenitydojo.cart;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Random;
7+
8+
public class Cart {
9+
10+
List<Item> contents = new ArrayList<>();
11+
12+
public void add(Item item) {
13+
contents.add(item);
14+
}
15+
16+
public List<Item> getItems() {
17+
return contents;
18+
}
19+
20+
public Item getItem(int index) {
21+
return contents.get(index);
22+
}
23+
24+
Random random = new Random();
25+
public Item getItemWithName(String expectedItemName) throws NoSuchItemException, ItemNotReadyException {
26+
27+
if (random.nextBoolean()) {
28+
throw new ItemNotReadyException("Item with name " + expectedItemName + " not ready yet");
29+
}
30+
31+
for(Item item : contents) {
32+
if (item.isCalled(expectedItemName)) {
33+
return item;
34+
}
35+
}
36+
throw new NoSuchItemException("No such item with name " + expectedItemName);
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.serenitydojo.cart;
2+
3+
public class Item {
4+
private String name;
5+
6+
public Item(String name) {
7+
this.name = name;
8+
}
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public boolean isCalled(String expectedItemName) {
15+
return name.equalsIgnoreCase(expectedItemName.trim());
16+
}
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.serenitydojo.cart;
2+
3+
public class ItemNotReadyException extends Throwable {
4+
public ItemNotReadyException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.serenitydojo.cart;
2+
3+
public class NoSuchItemException extends Exception {
4+
public NoSuchItemException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.serenitydojo.cart;
2+
3+
public class TheItem {
4+
private Cart cart;
5+
6+
public TheItem(Cart cart) {
7+
this.cart = cart;
8+
}
9+
10+
public static TheItem inTheCart(Cart cart) {
11+
return new TheItem(cart);
12+
}
13+
14+
public Item withName(String itemName) {
15+
try {
16+
return cart.getItemWithName(itemName);
17+
} catch (NoSuchItemException e) {
18+
e.printStackTrace();
19+
} catch (ItemNotReadyException e) {
20+
e.printStackTrace();
21+
}
22+
return null;
23+
}
24+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.serenitydojo.enums;
2+
3+
import com.serenitydojo.cart.*;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
/**
11+
* In a shopping cart we should be able to
12+
* - add one item
13+
* - add multiple items
14+
* - add multiple quantities of the same item
15+
* - get a product description from the catalog
16+
*/
17+
public class WhenAddingStuffToACart {
18+
19+
@Test
20+
public void iCanAddASingleItem() {
21+
Cart cart = new Cart();
22+
Item item = new Item("bread");
23+
24+
cart.add(item);
25+
26+
assertThat(cart.getItems()).contains(item);
27+
}
28+
29+
@Test
30+
public void ICanGetAnItemFromTheList() {
31+
Cart cart = new Cart();
32+
Item item = new Item("bread");
33+
cart.add(item);
34+
35+
assertThat(cart.getItem(0)).isEqualTo(item);
36+
}
37+
38+
@Test
39+
public void iCanGetAnItemByName() throws NoSuchItemException, ItemNotReadyException {
40+
Cart cart = new Cart();
41+
Item item = new Item("bread");
42+
cart.add(item);
43+
44+
assertThat(cart.getItemWithName("bread")).isEqualTo(item);
45+
}
46+
47+
@Test(expected = NoSuchItemException.class)
48+
public void noMatchingItemIsInTheCart() {
49+
Cart cart = new Cart();
50+
Item item = new Item("bread");
51+
cart.add(item);
52+
53+
Item foundItem = TheItem.inTheCart(cart).withName("cheese");
54+
55+
assertThat(foundItem).isEqualTo(item);
56+
}
57+
}
Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
package com.serenitydojo.exceptions;
22

3-
import org.assertj.core.api.Assertions;
4-
import org.junit.Test;
5-
6-
import java.net.MalformedURLException;
7-
8-
import static org.assertj.core.api.Assertions.assertThat;
9-
103
public class WhenWorkingWithExceptions {
114

12-
@Test
13-
public void shouldShowTheLengthOfAString() {
14-
StringProcessor stringProcessor = new StringProcessor();
15-
16-
String result = stringProcessor.showLengthOf("some string");
17-
18-
assertThat(result).isEqualTo("some string has a length of 11");
19-
}
20-
21-
@Test
22-
public void shouldShowZeroForNullStrings() {
23-
24-
StringProcessor stringProcessor = new StringProcessor();
25-
26-
String result = stringProcessor.showLengthOf(null);
27-
28-
assertThat(result).isEqualTo("null has a length of 0");
29-
}
30-
31-
@Test(expected = TestEnvironmentUnavailableException.class)
32-
public void shouldFindThePort() {
33-
StringProcessor stringProcessor = new StringProcessor();
34-
35-
stringProcessor.getPortOf("A:https://www.google.com");
36-
}
375
}

0 commit comments

Comments
 (0)