@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user