Skip to content

Commit

Permalink
fix: Fixed an issue where tests that failed when using the @test expe…
Browse files Browse the repository at this point in the history
…cted attribute where not being reported correctly.
  • Loading branch information
wakaleo committed May 14, 2016
1 parent cc81c5f commit b491091
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
Expand Up @@ -66,7 +66,8 @@ private boolean isExpected(Failure failure) {
Method testMethod = failure.getDescription().getTestClass().getMethod(failure.getDescription().getMethodName());
Test testAnnotation = testMethod.getAnnotation(Test.class);
if (testAnnotation.expected() != null) {
return (failure.getException().getClass().isAssignableFrom(testAnnotation.expected()));
return (testAnnotation.expected().isAssignableFrom(failure.getException().getClass()));
// return (failure.getException().getClass().isAssignableFrom(testAnnotation.expected()));
}
} catch (NoSuchMethodException e) {
return false;
Expand Down
@@ -0,0 +1,132 @@
package net.thucydides.junit.runners

import net.serenitybdd.junit.runners.SerenityRunner
import net.thucydides.core.annotations.Step
import net.thucydides.core.annotations.Steps
import net.thucydides.core.model.TestResult
import net.thucydides.core.util.MockEnvironmentVariables
import net.thucydides.core.webdriver.SystemPropertiesConfiguration
import net.thucydides.core.webdriver.WebDriverFactory
import net.thucydides.core.webdriver.WebdriverInstanceFactory
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.junit.runner.notification.RunNotifier
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.htmlunit.HtmlUnitDriver
import spock.lang.Specification

class WhenHandlingFailingTests extends Specification {

def firefoxDriver = Mock(FirefoxDriver)
def htmlUnitDriver = Mock(HtmlUnitDriver)
def webdriverInstanceFactory = Mock(WebdriverInstanceFactory)
def environmentVariables = new MockEnvironmentVariables()
def configuration = new SystemPropertiesConfiguration(environmentVariables)
def webDriverFactory = new WebDriverFactory(webdriverInstanceFactory, environmentVariables)
File temporaryDirectory

@Rule
TemporaryFolder temporaryFolder

def setup() {
temporaryDirectory = temporaryFolder.newFolder()
webdriverInstanceFactory.newFirefoxDriver(_) >> firefoxDriver
webdriverInstanceFactory.newHtmlUnitDriver(_) >> htmlUnitDriver
}


static class SomeSteps {
@Step
public void myFailingStep() {
throw new IllegalStateException()

}

@Step
public void myUnexpectedlyFailingStep() {
throw new UnknownError()

}
}

@RunWith(SerenityRunner)
static class ATestWithAnExpectedExceptionInAStep {

@Steps
SomeSteps mysteps;

@Test(expected=IllegalStateException)
public void shouldThrowAnIllegalStateException() {
mysteps.myFailingStep();
}
}

def "should report tests with an expected exception in a step as passing"() {
given:
def runner = new SerenityRunner(ATestWithAnExpectedExceptionInAStep)
when:
runner.run(new RunNotifier())
then:
runner.testOutcomes.get(0).result == TestResult.SUCCESS
}

@RunWith(SerenityRunner)
static class ATestWithAnUnexpectedExceptionInAStep {

@Steps
SomeSteps mysteps;

@Test(expected=IllegalStateException)
public void shouldThrowAnIllegalStateException() {
mysteps.myUnexpectedlyFailingStep();
}
}

def "should report tests with an unexpected exception in a step as failing"() {
given:
def runner = new SerenityRunner(ATestWithAnUnexpectedExceptionInAStep)
when:
runner.run(new RunNotifier())
then:
runner.testOutcomes.get(0).result == TestResult.ERROR
}

@RunWith(SerenityRunner)
static class ATestWithAnExpectedException {

@Test(expected=IllegalStateException)
public void shouldThrowAnIllegalStateException() {
throw new IllegalStateException()
}
}

def "should report tests with an expected exception as passing"() {
given:
def runner = new SerenityRunner(ATestWithAnExpectedException)
when:
runner.run(new RunNotifier())
then:
runner.testOutcomes.get(0).result == TestResult.SUCCESS
}

@RunWith(SerenityRunner)
static class ATestWithAnUnexpectedException {

@Test(expected=IllegalStateException)
public void shouldThrowAnIllegalStateException() {
throw new NullPointerException()
}
}

def "should report tests with an unexpected exception as failing"() {
given:
def runner = new SerenityRunner(ATestWithAnUnexpectedException)
when:
runner.run(new RunNotifier())
then:
runner.testOutcomes.get(0).result == TestResult.ERROR
}

}
@@ -0,0 +1,14 @@
package net.thucydides.junit.runners.samples;

import net.serenitybdd.junit.runners.SerenityRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(SerenityRunner.class)
public class ATestWithAnExpectedException {

@Test(expected = IllegalStateException.class)
public void shouldThrowAnIllegalStateException() {
throw new NullPointerException();
}
}

0 comments on commit b491091

Please sign in to comment.