@Test
public void shouldInsertPetIntoDatabaseAndGenerateId() {
Owner owner6 = this.clinicService.findOwnerById(6);
int found = owner6.getPets().size();
Pet pet = new Pet();
pet.setName("bowser");
Collection<PetType> types = this.clinicService.findPetTypes();
pet.setType(EntityUtils.getById(types, PetType.class, 2));
pet.setBirthDate(new DateTime());
owner6.addPet(pet);
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
this.clinicService.savePet(pet);
this.clinicService.saveOwner(owner6);
owner6 = this.clinicService.findOwnerById(6);
assertThat(owner6.getPets().size()).isEqualTo(found + 1);
// checks that id has been generated
assertThat(pet.getId()).isNotNull();
}
Example from github.com/spring-projects/spring-petclinic
Scenario: Pets can be assigned to pet owners
Given a pet owner
And a dog
When assigning the pet to the pet owner
Then the pet owner owns an additional pet
import org.junit.Test;
import com.tngtech.jgiven.junit.ScenarioTest;
public class FindOwnerTest
extends ScenarioTest<GivenOwner, WhenSearching, ThenOwner> {
@Test
public void owners_can_be_found_by_last_name() {
given().an_owner_with_last_name("Smith");
when().searching_for("Smith");
then().exactly_the_given_owner_is_found();
}
}
@JGivenStage // only needed when using Spring
public class WhenSearching extends Stage<WhenSearching> {
@Autowired
ClinicService clinicService;
@ScenarioState
Owner owner;
public WhenSearching searching_for(String name) {
owner = this.clinicService.findOwnerByName(name);
return this;
}
}
Owners can be found by last name
Given an owner with last name "Smith"
When searching for "Smith"
Then exactly the given owner is found
@Test
@DataProvider({
"Smith, 1",
"Davis, 0",
"Sm, 1"})
public void should_find_owner_by_last_name( String searchTerm,
int numberOfResults ) {
given().an_owner_with_last_name("Smith");
when().searching_for(searchTerm);
then().exactly_$_owner_is_found(numberOfResults);
}
Uses the DataProviderRunner (github.com/TNG/junit-dataprovider). Should find owner by last name
Given an owner with last name Smith
When searching for <searchTerm>
Then exactly <numberOfResults> owner is found
Cases:
| # | searchTerm | numberOfResults | Status |
+---+------------+-----------------+---------+
| 1 | Smith | 1 | Success |
| 2 | Davis | 0 | Success |
| 3 | Sm | 1 | Success |
@Format( value = BooleanFormatter.class, args = { "on", "off" } )
@Retention( RetentionPolicy.RUNTIME )
@interface OnOff {}
public SELF the_machine_is_$( @OnOff boolean onOrOff ) { ... }
given().the_machine_is_$( false );
Given the machine is off
public class GivenSteps extends Stage<GivenSteps> {
@ProvidedScenarioState
File temporaryFolder;
@BeforeScenario
void setupTemporaryFolder() {
temporaryFolder = ...
}
@AfterScenario
void deleteTemporaryFolder() {
temporaryFolder.delete();
}
}
public class TemporaryFolderRule {
File temporaryFolder;
public void before() {
temporaryFolder = ...
}
public void after() {
temporaryFolder.delete();
}
}
public class GivenSteps extends Stage<GivenSteps> {
@ScenarioRule
TemporaryFolderRule rule = new TemporaryFolderRule();
}
public class GivenCustomer extends Stage<GivenSteps> {
CustomerBuilder builder;
@ProvidedScenarioState
Customer customer;
public void a_customer() {
builder = new CustomerBuilder();
}
public void with_age(int age) {
builder.withAge(age);
}
@AfterStage
void buildCustomer() {
customer = builder.build();
}
}
@Test @FeatureEmail
void the_customer_gets_an_email_when_ordering_a_book() {
...
}
@Test @Story( "ABC-123" )
void the_customer_gets_an_email_when_ordering_a_book() { ... }
@Hidden
public SELF doSomethingTechnical() { ... }
@ExtendedDescription("The Hitchhiker's Guide to the Galaxy, "
+ "by default the book is not on stock" )
public SELF a_book() { ... }
public class Html5ReportStage {
@ExpectedScenarioState
protected CurrentStep currentStep; // provided by JGiven
protected void takeScreenshot() {
String base64 = ( (TakesScreenshot) webDriver )
.getScreenshotAs( OutputType.BASE64 );
currentStep.addAttachment(
Attachment.fromBase64( base64, MediaType.PNG )
.withTitle( "Screenshot" ) );
}
}