An error in junit test kotlin

Hi
I wrote a junit unit test with java . and everything was ok .
Now i want to write same junit test with kotlin but i face by this error :

org.junit.internal.runners.rules.ValidationError: The @ClassRule 'rule' must be static.

And this is the line that has error :

@ClassRule
@Rule
var rule = TestCoverageProcessEngineRuleBuilder.create().build()

What should i do ?

Hi @rezza72,

the rule field needs to be static.

Maybe this helps: https://stackoverflow.com/questions/40352879/what-is-the-equivalent-of-java-static-final-fields-in-kotlin

Cheers,
Tassilo

1 Like

Hi @tasso94
Thanks for guiding .

I added companion object {
in top of the @ClassRule annotation and now i faced by this error :

The @ClassRule 'rule' must be public.

Actually i think var rule = TestCoverageProcessEngineRuleBuilder.create().build()
Is public and static in kotlin .
I think problem is related to rule .
Because in this topic :

mentioned if we use Spring boot , we shouldn’t use ProcessEngineRule .
Is that true or i got wrong meaning ?

If its advisable to use the engine depends on your test level. If you want to run an in memory test based on your bpmn and mocked delegates, it is still the way to go. If you are more on the integration test level, running the complete springApplicationTest is the better approach. Both are valid and useful in my opinion.

Regarding classrule in kotlin: you are probably missing the JvmField annotation:

companion object {
        @ClassRule @JvmField
        val camunda = new ProcessEngineRule()
}

should work (see: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-field/)

1 Like

Hi @jangalinski
I used that suggested code in this format :

   @ClassRule
     @Rule
        @JvmField
        val rule = TestCoverageProcessEngineRuleBuilder.create().build()

And first error was solved . but now i faced by this error :

java.lang.Exception: No runnable methods

:frowning_face: :frowning_face:

Hm … I created a sample project: https://github.com/jangalinski/camunda-kotlin-test/blob/master/src/test/kotlin/TestCoverageTest.kt

It somehow fails with

INFORMATION: deploy and start$camunda_kotlin_test test method coverage is 0.6
java.lang.IllegalArgumentException: Illegal group reference

but at least it seems to run the process and measure something.

Could you check this out?

1 Like

Hi @jangalinski

I faced by error again . ( however i use mysql )
For using complete(task(processInstance) we should use camunda.processEngine.runtimeService.startProcessInstanceByKey("my-process")
instead of
val processInstance = camunda.runtimeService.startProcessInstanceByKey("my-process")
format ?

if you look at the implementation of camunda asserts static helpers you will notice that one way or the other they all go down to calling the services camunda provides. so basically both approaches are vaild.

1 Like

Hi @jangalinski
Sorry, but i faced by another error again :

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [activiti.cfg.xml]; nested exception is java.io.FileNotFoundException: class path resource [activiti.cfg.xml] cannot be opened because it does not exist

This is my rule definition section :

companion object {
        @Rule
        @ClassRule
        @JvmField
        val rule = TestCoverageProcessEngineRuleBuilder.create().build()
    }

And this is my test :

 @Test
  @Deployment(resources = ["my-process.bpmn"])
  internal fun `deploy and start`() {
  val processInstance = rule.runtimeService.createProcessInstanceByKey(PROCESS_DEFINITION_KEY)
                }

Please guide me .
thanks

The rule tries to configure the test-engine. Default source for config is the xml file you mentioned. It’s missing, so setting up the rule fails.
There’s plenty of documentation on engine configuration, you should check it out

1 Like

Hi @jangalinski

Should i write a configuration for TestCoverageProcessEngineRuleBuilder.create().build() in my configuration file ?
Something like this :

 @Bean
    open fun rule(): DataSource {
        return TestCoverageProcessEngineRuleBuilder.create().build()
    }

Please help me .
I’m newbie in Camunda and codding .