Skip to content

Commit 339cf6a

Browse files
NamozagKevinGilmore
authored andcommitted
BAEL-1278 (eugenp#3214)
* BAEL-1278 * Rename methods names
1 parent b2ea831 commit 339cf6a

7 files changed

Lines changed: 306 additions & 0 deletions

File tree

‎testing-modules/testing/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@
9494
<version>1.5</version>
9595
<scope>test</scope>
9696
</dependency>
97+
<dependency>
98+
<groupId>org.javalite</groupId>
99+
<artifactId>javalite-common</artifactId>
100+
<version>${javalite.version}</version>
101+
</dependency>
97102
</dependencies>
98103
<build>
99104
<plugins>
@@ -162,5 +167,6 @@
162167
<truth.version>0.32</truth.version>
163168
<jUnitParams.version>1.1.0</jUnitParams.version>
164169
<jgotesting.version>0.12</jgotesting.version>
170+
<javalite.version>1.4.13</javalite.version>
165171
</properties>
166172
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.jspec;
2+
3+
public abstract class Animal {
4+
5+
protected String name;
6+
7+
public Animal(String name) {
8+
this.name = name;
9+
}
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
@Override
16+
public int hashCode() {
17+
final int prime = 31;
18+
int result = 1;
19+
result = prime * result + ((name == null) ? 0 : name.hashCode());
20+
return result;
21+
}
22+
23+
@Override
24+
public boolean equals(Object obj) {
25+
if (this == obj)
26+
return true;
27+
if (obj == null)
28+
return false;
29+
if (getClass() != obj.getClass())
30+
return false;
31+
Animal other = (Animal) obj;
32+
if (name == null) {
33+
if (other.name != null)
34+
return false;
35+
} else if (!name.equals(other.name))
36+
return false;
37+
return true;
38+
}
39+
40+
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.jspec;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
public class Cage {
8+
9+
private Set<Animal> animals = new HashSet<>();
10+
11+
public void put(Animal animal) {
12+
animals.add(animal);
13+
}
14+
15+
public void put(Animal... animals) {
16+
this.animals.addAll(Arrays.asList(animals));
17+
}
18+
19+
public Animal release(Animal animal) {
20+
return animals.remove(animal) ? animal : null;
21+
}
22+
23+
public void open() {
24+
animals.clear();
25+
}
26+
27+
public boolean hasAnimals() {
28+
return animals.size() > 0;
29+
}
30+
31+
public boolean isEmpty() {
32+
return animals.isEmpty();
33+
}
34+
35+
public Set<Animal> getAnimals() {
36+
return this.animals;
37+
}
38+
39+
public int size() {
40+
return animals.size();
41+
}
42+
43+
@Override
44+
public String toString() {
45+
return "Cage [animals=" + animals + "]";
46+
}
47+
48+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.jspec;
2+
3+
public class Cat extends Animal {
4+
5+
public Cat(String name) {
6+
super(name);
7+
}
8+
9+
@Override
10+
public String toString() {
11+
return "Cat [name=" + name + "]";
12+
}
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.jspec;
2+
3+
public class Dog extends Animal {
4+
5+
public Dog(String name) {
6+
super(name);
7+
}
8+
9+
@Override
10+
public String toString() {
11+
return "Dog [name=" + name + "]";
12+
}
13+
14+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.baeldung.jspec;
2+
3+
import static org.javalite.test.jspec.JSpec.$;
4+
import static org.javalite.test.jspec.JSpec.expect;
5+
import static org.javalite.test.jspec.JSpec.the;
6+
7+
import java.util.Set;
8+
9+
import org.javalite.test.jspec.DifferenceExpectation;
10+
import org.junit.Test;
11+
12+
public class CageUnitTest {
13+
14+
Cat tomCat = new Cat("Tom");
15+
Cat felixCat = new Cat("Felix");
16+
Dog boltDog = new Dog("Bolt");
17+
Cage cage = new Cage();
18+
19+
20+
@Test
21+
public void puttingAnimals_shouldIncreaseCageSize() {
22+
// When
23+
cage.put(tomCat, boltDog);
24+
25+
// Then
26+
the(cage.size()).shouldEqual(2);
27+
}
28+
29+
@Test
30+
public void releasingAnimals_shouldDecreaseCageSize() {
31+
// When
32+
cage.put(tomCat, boltDog);
33+
cage.release(tomCat);
34+
35+
// Then
36+
the(cage.size()).shouldEqual(1);
37+
}
38+
39+
@Test
40+
public void puttingAnimals_shouldLeaveThemInsideTheCage() {
41+
// When
42+
cage.put(tomCat, boltDog);
43+
44+
// Then
45+
the(cage).shouldHave("animals");
46+
}
47+
48+
@Test
49+
public void openingTheCage_shouldReleaseAllAnimals() {
50+
// When
51+
cage.put(tomCat, boltDog);
52+
53+
// Then
54+
the(cage).shouldNotBe("empty");
55+
56+
// When
57+
cage.open();
58+
59+
// Then
60+
the(cage).shouldBe("empty");
61+
the(cage.isEmpty()).shouldBeTrue();
62+
}
63+
64+
@Test
65+
public void comparingTwoDogs() {
66+
// When
67+
Dog firstDog = new Dog("Rex");
68+
Dog secondDog = new Dog("Rex");
69+
70+
// Then
71+
$(firstDog).shouldEqual(secondDog);
72+
$(firstDog).shouldNotBeTheSameAs(secondDog);
73+
}
74+
75+
@Test
76+
public void puttingCatsOnly_shouldLetCageAnimalsToContainCats() {
77+
// When
78+
cage.put(tomCat, felixCat);
79+
80+
// Then
81+
Set<Animal> animals = cage.getAnimals();
82+
the(animals).shouldContain(tomCat);
83+
the(animals).shouldContain(felixCat);
84+
the(animals).shouldNotContain(boltDog);
85+
}
86+
87+
@Test
88+
public void puttingCatsOnly_shouldLetCageToContainCats() {
89+
// When
90+
cage.put(tomCat, felixCat);
91+
92+
// Then
93+
// Check with toString of the tested objects
94+
the(cage).shouldContain(tomCat);
95+
the(cage).shouldContain(felixCat);
96+
the(cage).shouldNotContain(boltDog);
97+
}
98+
99+
@Test
100+
public void puttingMoreAnimals_shouldChangeSize() {
101+
// When
102+
cage.put(tomCat, boltDog);
103+
104+
// Then
105+
expect( new DifferenceExpectation<Integer>(cage.size()) {
106+
107+
@Override
108+
public Integer exec() {
109+
cage.release(tomCat);
110+
return cage.size();
111+
}
112+
} );
113+
}
114+
115+
116+
@Test
117+
public void releasingTheDog_shouldReleaseAnAnimalOfDogType() {
118+
// When
119+
cage.put(boltDog);
120+
Animal releasedAnimal = cage.release(boltDog);
121+
122+
// Then
123+
the(releasedAnimal).shouldNotBeNull();
124+
the(releasedAnimal).shouldBeA(Dog.class);
125+
}
126+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.jspec;
2+
3+
import static org.javalite.test.jspec.JSpec.$;
4+
import static org.javalite.test.jspec.JSpec.a;
5+
import static org.javalite.test.jspec.JSpec.expect;
6+
import static org.javalite.test.jspec.JSpec.it;
7+
import static org.javalite.test.jspec.JSpec.the;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
import org.javalite.test.jspec.ExceptionExpectation;
13+
import org.junit.Test;
14+
15+
public class JSpecUnitTest {
16+
17+
@Test
18+
public void onePlusTwo_shouldEqualThree() {
19+
$(1 + 2).shouldEqual(3);
20+
a(1 + 2).shouldEqual(3);
21+
the(1 + 2).shouldEqual(3);
22+
it(1 + 2).shouldEqual(3);
23+
}
24+
25+
@Test
26+
public void messageShouldContainJSpec() {
27+
String message = "Welcome to JSpec demo";
28+
// The message should not be empty
29+
the(message).shouldNotBe("empty");
30+
// The message should contain JSpec
31+
the(message).shouldContain("JSpec");
32+
}
33+
34+
public void colorsListShouldContainRed() {
35+
List<String> colorsList = Arrays.asList("red", "green", "blue");
36+
$(colorsList).shouldContain("red");
37+
}
38+
39+
public void guessedNumberShouldEqualHiddenNumber() {
40+
Integer guessedNumber = 11;
41+
Integer hiddenNumber = 11;
42+
43+
$(guessedNumber).shouldEqual(hiddenNumber);
44+
$(guessedNumber).shouldNotBeTheSameAs(hiddenNumber);
45+
}
46+
47+
@Test
48+
public void dividingByThero_shouldThrowArithmeticException() {
49+
expect(new ExceptionExpectation<ArithmeticException>(ArithmeticException.class) {
50+
@Override
51+
public void exec() throws ArithmeticException {
52+
System.out.println(1 / 0);
53+
}
54+
} );
55+
}
56+
57+
}

0 commit comments

Comments
 (0)