SlingContext for JUnit is giving error

Avatar

Avatar

karthikbhat3624

Avatar

karthikbhat3624

karthikbhat3624

20-05-2020

HI,

I'm trying to use 

@rule
public SlingContext context = new SlingContext();

 

But it is giving me error below...

java.lang.Exception: Field context must implement MethodRule
at org.junit.runners.BlockJUnit4ClassRunner.validateRuleField(BlockJUnit4ClassRunner.java:188)
at org.junit.runners.BlockJUnit4ClassRunner.validateFields(BlockJUnit4ClassRunner.java:183)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:123)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:269)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:66)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:58)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl$1.<init>(JUnit45AndHigherRunnerImpl.java:23)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.<init>(JUnit45AndHigherRunnerImpl.java:23)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.mockito.internal.runners.util.RunnerProvider.newInstance(RunnerProvider.java:39)
at org.mockito.internal.runners.RunnerFactory.create(RunnerFactory.java:28)
at org.mockito.runners.MockitoJUnitRunner.<init>(MockitoJUnitRunner.java:57)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 

Accepted Solutions (1)

Accepted Solutions (1)

Answers (3)

Answers (3)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
Jörg_Hoh
Employee

21-05-2020

Is it it really "@rule" or rather "@Rule"? Otherwise this looks ok...

Avatar

Avatar

ArpitVarshney

MVP

Avatar

ArpitVarshney

MVP

ArpitVarshney
MVP

21-05-2020

Hi, @karthikbhat3624 This might be a ClassLoader conflict. Do you have more than one version of JUnit in your project?

 

Regards,

Arpit

Avatar

Avatar

Veena_Vikram

MVP

Avatar

Veena_Vikram

MVP

Veena_Vikram
MVP

21-05-2020

agree with @Jörg_Hoh . It should be @Rule and the import should be org.junit.Rule. It should work perfectly 

 

Thanks

Veena