* BAEL-1278

* Rename methods names
This commit is contained in:
Hany Ahmed 2017-12-12 06:39:39 +02:00 committed by KevinGilmore
parent b2ea831d83
commit 339cf6a7d0
7 changed files with 306 additions and 0 deletions

View File

@ -94,6 +94,11 @@
<version>1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.javalite</groupId>
<artifactId>javalite-common</artifactId>
<version>${javalite.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
@ -162,5 +167,6 @@
<truth.version>0.32</truth.version>
<jUnitParams.version>1.1.0</jUnitParams.version>
<jgotesting.version>0.12</jgotesting.version>
<javalite.version>1.4.13</javalite.version>
</properties>
</project>

View File

@ -0,0 +1,41 @@
package com.baeldung.jspec;
public abstract class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Animal other = (Animal) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}

View File

@ -0,0 +1,48 @@
package com.baeldung.jspec;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Cage {
private Set<Animal> animals = new HashSet<>();
public void put(Animal animal) {
animals.add(animal);
}
public void put(Animal... animals) {
this.animals.addAll(Arrays.asList(animals));
}
public Animal release(Animal animal) {
return animals.remove(animal) ? animal : null;
}
public void open() {
animals.clear();
}
public boolean hasAnimals() {
return animals.size() > 0;
}
public boolean isEmpty() {
return animals.isEmpty();
}
public Set<Animal> getAnimals() {
return this.animals;
}
public int size() {
return animals.size();
}
@Override
public String toString() {
return "Cage [animals=" + animals + "]";
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.jspec;
public class Cat extends Animal {
public Cat(String name) {
super(name);
}
@Override
public String toString() {
return "Cat [name=" + name + "]";
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.jspec;
public class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
public String toString() {
return "Dog [name=" + name + "]";
}
}

View File

@ -0,0 +1,126 @@
package com.baeldung.jspec;
import static org.javalite.test.jspec.JSpec.$;
import static org.javalite.test.jspec.JSpec.expect;
import static org.javalite.test.jspec.JSpec.the;
import java.util.Set;
import org.javalite.test.jspec.DifferenceExpectation;
import org.junit.Test;
public class CageUnitTest {
Cat tomCat = new Cat("Tom");
Cat felixCat = new Cat("Felix");
Dog boltDog = new Dog("Bolt");
Cage cage = new Cage();
@Test
public void puttingAnimals_shouldIncreaseCageSize() {
// When
cage.put(tomCat, boltDog);
// Then
the(cage.size()).shouldEqual(2);
}
@Test
public void releasingAnimals_shouldDecreaseCageSize() {
// When
cage.put(tomCat, boltDog);
cage.release(tomCat);
// Then
the(cage.size()).shouldEqual(1);
}
@Test
public void puttingAnimals_shouldLeaveThemInsideTheCage() {
// When
cage.put(tomCat, boltDog);
// Then
the(cage).shouldHave("animals");
}
@Test
public void openingTheCage_shouldReleaseAllAnimals() {
// When
cage.put(tomCat, boltDog);
// Then
the(cage).shouldNotBe("empty");
// When
cage.open();
// Then
the(cage).shouldBe("empty");
the(cage.isEmpty()).shouldBeTrue();
}
@Test
public void comparingTwoDogs() {
// When
Dog firstDog = new Dog("Rex");
Dog secondDog = new Dog("Rex");
// Then
$(firstDog).shouldEqual(secondDog);
$(firstDog).shouldNotBeTheSameAs(secondDog);
}
@Test
public void puttingCatsOnly_shouldLetCageAnimalsToContainCats() {
// When
cage.put(tomCat, felixCat);
// Then
Set<Animal> animals = cage.getAnimals();
the(animals).shouldContain(tomCat);
the(animals).shouldContain(felixCat);
the(animals).shouldNotContain(boltDog);
}
@Test
public void puttingCatsOnly_shouldLetCageToContainCats() {
// When
cage.put(tomCat, felixCat);
// Then
// Check with toString of the tested objects
the(cage).shouldContain(tomCat);
the(cage).shouldContain(felixCat);
the(cage).shouldNotContain(boltDog);
}
@Test
public void puttingMoreAnimals_shouldChangeSize() {
// When
cage.put(tomCat, boltDog);
// Then
expect( new DifferenceExpectation<Integer>(cage.size()) {
@Override
public Integer exec() {
cage.release(tomCat);
return cage.size();
}
} );
}
@Test
public void releasingTheDog_shouldReleaseAnAnimalOfDogType() {
// When
cage.put(boltDog);
Animal releasedAnimal = cage.release(boltDog);
// Then
the(releasedAnimal).shouldNotBeNull();
the(releasedAnimal).shouldBeA(Dog.class);
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.jspec;
import static org.javalite.test.jspec.JSpec.$;
import static org.javalite.test.jspec.JSpec.a;
import static org.javalite.test.jspec.JSpec.expect;
import static org.javalite.test.jspec.JSpec.it;
import static org.javalite.test.jspec.JSpec.the;
import java.util.Arrays;
import java.util.List;
import org.javalite.test.jspec.ExceptionExpectation;
import org.junit.Test;
public class JSpecUnitTest {
@Test
public void onePlusTwo_shouldEqualThree() {
$(1 + 2).shouldEqual(3);
a(1 + 2).shouldEqual(3);
the(1 + 2).shouldEqual(3);
it(1 + 2).shouldEqual(3);
}
@Test
public void messageShouldContainJSpec() {
String message = "Welcome to JSpec demo";
// The message should not be empty
the(message).shouldNotBe("empty");
// The message should contain JSpec
the(message).shouldContain("JSpec");
}
public void colorsListShouldContainRed() {
List<String> colorsList = Arrays.asList("red", "green", "blue");
$(colorsList).shouldContain("red");
}
public void guessedNumberShouldEqualHiddenNumber() {
Integer guessedNumber = 11;
Integer hiddenNumber = 11;
$(guessedNumber).shouldEqual(hiddenNumber);
$(guessedNumber).shouldNotBeTheSameAs(hiddenNumber);
}
@Test
public void dividingByThero_shouldThrowArithmeticException() {
expect(new ExceptionExpectation<ArithmeticException>(ArithmeticException.class) {
@Override
public void exec() throws ArithmeticException {
System.out.println(1 / 0);
}
} );
}
}