0

we're trying to use Gherkin/Cucumber for the Unit test. In maven project, we used to use JUnit/Jmockit to perform unit test with following format, and it works well.

Old Junit test class used to work:

@RunWith(JMockit.class)
public class UnitTestClass{
    @Mocked
    AClass mockedObject;

    @Test
    public void AMethodTest() {
        try {
            new NonStrictExpectations() {
                {
                    ExampleObject.callAMethod();
                    result = mockedObject;
                }
            };
                     Assert.assertEquals(xxxxx);
                catch(Exception e){
                }  
}

After starting to use Gherkin/Cucumber, we are writing Unit class with below two classes:

@RunWith(Cucumber.class)
@CucumberOptions (features = "src/test/java/feature/")
//@RunWith(JMockit.class)
public class UnitTestClass{
}


@RunWith(JMockit.class)
public class UnitTestClassSteps{

    @Given("^an input data id: '(\\d+)'\\.$")
    @Test
    public void a_data_ID(int arg1) throws Throwable {

            try {
                **new NonStrictExpectations() {**
                    {
                        ExampleObject.callAMethod();
                        result = mockedObject;
                    }
                };
                    Assert.assertEquals(xxxxx);
               catch(Exception e){
               } 
   }

Once we start run UnitTestClass, it jumps into UnitTestClassStep class, and failed at new NonStrictExpectations step, the error is:

java.lang.AssertionError: Exception while calling Given steps JMockit wasn't properly initialized; check that jmockit.jar precedes junit.jar in the classpath (if using JUnit; if not, check the documentation) at org.junit.Assert.fail(Assert.java:88)

So can we use Jmockit in Cucumber step test class? what's the best practice if we need to mock object in step test class?

Thanks!

GL.
  • 11
  • 1
  • 3
  • Have a look at this - http://stackoverflow.com/questions/2905735/jmockit-initialization-problem – Grasshopper Oct 01 '16 at 10:24
  • I tried the option 1, add -javaagent:path/to/your/jmockit/jmockit-xxx.jar in JVM environment, now "JMockit wasn't properly initialized" error is gone, however, there is new error: java.lang.AssertionError: Exception while calling Given steps Invalid place to record expectations at org.junit.Assert.fail(Assert.java:88), any ideas? thanks! – GL. Oct 03 '16 at 15:12
  • I know very little about jmockit but seems like your jmockit is not being initialized. Can you try with @Mocked annotation for the object you are mocking. – Grasshopper Oct 03 '16 at 16:12

1 Answers1

0

I've been using cucumber-java 1.2.5 with jMockit 1.3.0 and it worked like a charm. No hacks, not tricks, no complex setup whatsoever.

The only thing that happened is that jMockit itself complained to be declared before some other library, I think it was jUnit. It happened a few days ago, and it was an easy fix at the pom file, so I didn't care so much.

The test runner is using @RunWith(Cucumber.class), and I just need to instantiate the mocks before instantiating the class that uses the "real ones", which means, regular jMockit use case.

cbaldan
  • 522
  • 8
  • 16