Skip to content

Commit

Permalink
Merge pull request #43 from fcamblor/specs-based-test-refacto
Browse files Browse the repository at this point in the history
Some minor improvements in the way spec tests were written
  • Loading branch information
xhanin committed Aug 28, 2013
2 parents b5d0b77 + 13b7652 commit e69f9cc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package {{mainPackage}}.rest;

import {{mainPackage}}.AppServer;
import org.junit.ClassRule;
import org.junit.Test;
import restx.tests.RestxSpecRule;
import restx.tests.RestxSpecTestsRunner;
import restx.tests.FindSpecsIn;

@RunWith(RestxSpecTestsRunner.class)
@FindSpecsIn("specs/hello")
public class HelloResourceSpecTest {
@ClassRule
public static RestxSpecRule rule = new RestxSpecRule(
AppServer.WEB_INF_LOCATION,
AppServer.WEB_APP_LOCATION);
@Test
public void should_say_hello() throws Exception {
rule.runTest("specs/hello/should_say_hello.spec.yaml");
}
/**
* Useless, thanks to both @RunWith(RestxSpecTestsRunner.class) & @FindSpecsIn()
*
* @Rule
* public RestxSpecRule rule = new RestxSpecRule();
*
* @Test
* public void test_spec() throws Exception {
* rule.runTest(specTestPath);
* }
*/
}
63 changes: 33 additions & 30 deletions restx-server-testing/src/test/java/restx/servers/SessionsTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package restx.servers;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import restx.factory.NamedComponent;
import restx.server.WebServerSupplier;
import restx.specs.RestxSpec;
import restx.tests.RestxSpecRule;
import restx.tests.RestxSpecRunner;
import restx.tests.RestxSpecTests;

import java.io.IOException;
import java.util.List;
import java.util.Set;

import static com.google.common.collect.Lists.newArrayList;

/**
* @author fcamblor
Expand All @@ -19,41 +25,38 @@ public class SessionsTest {
@Rule
public RestxSpecRule rule;

private String name;
private WebServerSupplier webServerSupplier;

@Parameterized.Parameters(name="{0}") // Parameter name will be the component's name
public static Iterable<Object[]> data(){
// Fetching every webserversuppliers provided in classpath
return Collections2.transform(
RestxSpecRunner.defaultFactory().queryByClass(WebServerSupplier.class).find(),
new Function<NamedComponent<WebServerSupplier>, Object[]>() {
@Override
public Object[] apply(NamedComponent<WebServerSupplier> input) {
return new Object[]{ input.getName().getName(), input.getComponent() };
}
});
private final RestxSpec spec;
private final WebServerSupplier webServerSupplier;

@Parameterized.Parameters(name="{0}")
public static Iterable<Object[]> data() throws IOException {
Set<NamedComponent<WebServerSupplier>> webServerSuppliers = RestxSpecRunner.defaultFactory().queryByClass(WebServerSupplier.class).find();
List<RestxSpec> specs = RestxSpecTests.findSpecsIn("specs/sessions");

List<Object[]> data = newArrayList();
for(NamedComponent<WebServerSupplier> webServerSupplierNamedComponent : webServerSuppliers){
for(RestxSpec restxSpec: specs){
data.add(new Object[]{
String.format("spec [%s] with server %s",
restxSpec.getTitle(),
webServerSupplierNamedComponent.getName().getName()),
restxSpec,
webServerSupplierNamedComponent.getComponent() });
}
}
return data;
}

public SessionsTest(String name, WebServerSupplier webServerSupplier){
this.name = name;
// name param is only used for the @Parameters' name attribute
public SessionsTest(String name, RestxSpec spec, WebServerSupplier webServerSupplier){
this.webServerSupplier = webServerSupplier;
this.spec = spec;
this.rule = new RestxSpecRule(this.webServerSupplier);
}


@Test
public void should_authentication_be_successful() throws Exception {
rule.runTest("specs/sessions/should_authentication_be_successful.spec.yaml");
}

@Test
public void should_authentication_be_in_failure() throws Exception {
rule.runTest("specs/sessions/should_authentication_be_in_failure.spec.yaml");
}

@Test
public void should_disconnection_be_successful() throws Exception {
rule.runTest("specs/sessions/should_disconnection_be_successful.spec.yaml");
public void should_server_scenario_be_ok() throws Exception {
this.rule.runTest(this.spec);
}
}
12 changes: 12 additions & 0 deletions restx-specs-tests/src/main/java/restx/tests/RestxSpecTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package restx.tests;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.util.ClasspathHelper;
Expand All @@ -17,6 +19,16 @@
* A list of specs to be run as tests by a RestxSpecTestsRunner.
*/
public class RestxSpecTests {

public static Iterable<Object[]> specsAsParametersIn(String location) throws IOException {
return Collections2.transform(findSpecsIn(location), new Function<RestxSpec, Object[]>() {
@Override
public Object[] apply(RestxSpec restxSpec) {
return new Object[]{restxSpec};
}
});
}

public static List<RestxSpec> findSpecsIn(String location) throws IOException {
RestxSpecLoader loader = new RestxSpecLoader();

Expand Down

0 comments on commit e69f9cc

Please sign in to comment.