-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use of BeforeAll/AfterAll in Kotlin-based Cucumber suite does not work as expected #2934
Comments
As a work around could you check if it works when you declare the functions at package level instead? |
Ah, why didn't I think of that... A package method does indeed work, and is a viable workaround (for me at least). Thank you for the suggestion. |
Looks like it is also still impossible to accurately detect companion objects in Java without depending on So probably worth adding a note to the documentation of |
I found this also works object AnObject {
@BeforeAll
@JvmStatic
fun beforeAll() {
// code here
}
@AfterAll
@JvmStatic
fun afterAll() {
// code here
}
} You can also have a class inside an object such as this: object AnObject {
@BeforeAll
@JvmStatic
fun beforeAll() {
// code here
}
@AfterAll
@JvmStatic
fun afterAll() {
// code here
}
class AClass {
@Before
fun beforeEachScenario(scenario: Scenario) {
// code here
}
}
} Which does allow you have reference private vals and vars inside the parent object. |
It would be great to be able to mix @BeforeAll and @before in the same kotlin class using the companion object, however as the original poster @ejbartelds pointed out, this results in an exception.
class Hooks {
@Before
fun beforeEachScenario(scenario: Scenario) {
// Use private val in before hook
assert(setInBeforeAll == 1)
}
companion object {
private var setInBeforeAll: Int = 0
@BeforeAll
@JvmStatic
fun beforeAll() {
setInBeforeAll = 1
}
@AfterAll
@JvmStatic
fun afterAll() {
// code here
}
}
} This would allow you to have private values in the companion object that can be used in the @before steps. |
@CraftyFella unfortunately it is not possible to access a companion object without |
π What did you see?
@BeforeAll
in a Kotlin-based Cucumber suite did not work as expected, even though placed in acompanion object
and annotated@JvmStatic
β What did you expect to see?
I expected the
@JvmStatic
and@BeforeAll
annotated method to be picked up by Cucumber without problems, as a before-all initializer method.π¦ Which tool/library version are you using?
cucumber-java 7.20.1 kotlin 2.0.x
π¬ How could we reproduce it?
See screenhots: in Kotlin, write a suite or a test and have a beforeAll method. See the error appear.
Debug this and set a breakpoint in
io.cucumber.java.MethodScanner::scan
; you'll see thebeforeAll
method is "seen" twice, once as a static final method (whish is OK) and once as a non-final method (in the companion object), which triggers the error.π Any additional context?
No response
The text was updated successfully, but these errors were encountered: