1

I am looking for a way to trigger a specific method in my JUnit class before and after a specific scenario. I see that it is possible to call a function, but is there any way to 'bind' them with the associated Java class?

Example Java test class:

@Runwith(Karate.class)
public class MyTest {
    public void runThisBeforeScenarioOne() {
        // Run this before scenario one
    }

    public void runThisAfterScenarioOne() {
        // Run this after scenario one
    }

    public void runThisBeforeScenarioTwo() {
        // Run this before scenario two
    }

    public void runThisAfterScenarioTwo() {
        // Run this after scenario two
    }   
}

Feature file:

Feature: my test

Background:
    * url 'http://myurl.com'

    Scenario: one
        Given request {"example": "example"}
        When method post
        Then status 200
        And match response == {"result":"example"}

    Scenario: two
        Given request {"example": "example"}
        When method post
        Then status 200
        And match response == {"result":"example"}

1 Answers1

0

If you want to call-in to Java, look at the ExecutionHook. It is undocumented and may undergo minor changes as it evolves - but I think this is what you are looking for: https://stackoverflow.com/a/59080128/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248