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!