Reputation: 11
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!
Upvotes: 0
Views: 3615
Reputation: 553
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.
Upvotes: 0