Skip to content
This repository was archived by the owner on Sep 22, 2020. It is now read-only.

Commit

Permalink
simple cucumber bdd tests for active objects
Browse files Browse the repository at this point in the history
  • Loading branch information
clouless committed Oct 13, 2018
1 parent 562dd1f commit d9371a8
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
21 changes: 20 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -137,6 +143,19 @@
<version>1.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>4.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ut.com.comsysto.poc.ao.service.features;

import com.comsysto.poc.ao.model.OwnerEntity;
import com.comsysto.poc.ao.service.PetAndOwnerDataAccessServiceImpl;
import cucumber.api.java8.En;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class Feature_OwnersAndPets_Scenario_FindOwnersWithPets implements En {

// Dependency
private PetAndOwnerDataAccessServiceImpl petAndOwnerDataAccessService;
// Subject
private OwnerEntity[] ownerEntities;

public Feature_OwnersAndPets_Scenario_FindOwnersWithPets() {
Given("PetAndOwnerDataAccessService to query for pets and owners", () -> {
petAndOwnerDataAccessService = Feature_OwnersAndPets_Test.petAndOwnerDataAccessService;
assertThat(this.petAndOwnerDataAccessService, is(notNullValue()));
});
When("we query the database via method getOwnersWhoHavePets", () -> {
ownerEntities = petAndOwnerDataAccessService.getOwnersWhoHavePets();
});
Then("we should only retrieve owners who actually have pets", () -> {
assertThat(ownerEntities, is(notNullValue()));
assertThat(ownerEntities.length, is(1));
assertThat(ownerEntities[0].getName(), is(equalTo("jim")));
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ut.com.comsysto.poc.ao.service.features;

import com.atlassian.activeobjects.external.ActiveObjects;
import com.atlassian.activeobjects.test.TestActiveObjects;
import com.comsysto.poc.ao.model.OwnerEntity;
import com.comsysto.poc.ao.model.PetEntity;
import com.comsysto.poc.ao.service.PetAndOwnerDataAccessServiceImpl;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import net.java.ao.EntityManager;
import net.java.ao.test.converters.NameConverters;
import net.java.ao.test.jdbc.Data;
import net.java.ao.test.jdbc.DatabaseUpdater;
import net.java.ao.test.junit.ActiveObjectsJUnitRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
import ut.com.comsysto.poc.ao.service.seed.DatabaseSeedHelper;

import static org.junit.Assert.assertNotNull;

@RunWith(ActiveObjectsJUnitRunner.class)
@Data(Feature_OwnersAndPets_Test.CucumberRunnerTestDatabaseUpdater.class)
@NameConverters
public class Feature_OwnersAndPets_Test {

private EntityManager entityManager;
private ActiveObjects activeObjects;
// Make available to tests
public static PetAndOwnerDataAccessServiceImpl petAndOwnerDataAccessService;

@Before
public void setUp() throws Exception {
assertNotNull(entityManager);
activeObjects = new TestActiveObjects(entityManager);
petAndOwnerDataAccessService = new PetAndOwnerDataAccessServiceImpl(activeObjects);
}

public static class CucumberRunnerTestDatabaseUpdater implements DatabaseUpdater {
@Override
public void update(EntityManager entityManager) throws Exception {
entityManager.migrate(PetEntity.class, OwnerEntity.class);
DatabaseSeedHelper.seed(entityManager);
}
}

// ------

@Test
public void subRunner() throws Exception {
assert(JUnitCore.runClasses(SubRunner.class).wasSuccessful());
}

@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/ut/com/comsysto/poc/ao/service/features",
plugin = {"pretty"})
public static class SubRunner { }
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package ut.com.comsysto.poc.ao.service.seed;

import com.comsysto.poc.ao.model.OwnerEntity;
import com.comsysto.poc.ao.model.PetEntity;
import net.java.ao.EntityManager;

public class DatabaseSeedHelper {
public static void seed(EntityManager entityManager) throws Exception {
OwnerEntity owner = entityManager.create(OwnerEntity.class);
owner.setName("bob");
owner.save();

OwnerEntity owner2 = entityManager.create(OwnerEntity.class);
owner2.setName("jim");
owner2.save();

PetEntity pet = entityManager.create(PetEntity.class);
pet.setName("snorre");
pet.setType("CAT");
pet.setOwnerId((long) owner2.getID());
pet.save();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: Owners and Pets are stored in he Database
We can query the database for owners and pets

Scenario: find owners with pets
Given PetAndOwnerDataAccessService to query for pets and owners
When we query the database via method getOwnersWhoHavePets
Then we should only retrieve owners who actually have pets

0 comments on commit d9371a8

Please sign in to comment.