I am currently learning JUnit, and the nice new features it has. Specifically, I am working with a simple ParameterResolver, reproduced below
package com.junit.test.me.junit_testing;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
public class BankAccountParameterResolver implements ParameterResolver{
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
return parameterContext.getParameter().getType() == BankAccount.class;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
return new BankAccount(0,0);
}
}
However, when I go to use this ParameterResolver in my test (using the @ExtendsWith annotation), I get a strange ClassCastException
package com.junit.test.me.junit_testing;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
//This annotation gives the following error
//Type mismatch: cannot convert from Class<BankAccountParameterResolver> to Class<? extends Extension>[]
@ExtendWith(BankAccountParameterResolver.class)
class BankAccountDITest {
@ParameterizedTest
public void testDeposit(BankAccount testMe) {
testMe.deposit(500);
assertEquals(500, testMe.getBalance());
}
}
I think that somehow my project is importing two different JUnit versions, but I can't figure it out. My IDE is Eclipse, if that helps!
I tried following a simple online course for Junit 5, however I get a
ClassCastException
when I try to compile the test code. Any help would be appreciated, I've already tried restarting Eclipse and my machine, to no avail.
The problem was that somehow I had a BankAccountParameterResolver in a different location of my project. Once I made only one BankAccountParameterResolver exist, it worked fine.