我正在尝试将JUnit 5集成到Eclipse Oxygen3下。 该项目已经有Mockito 2。
我已经完成了https://www.baeldung.com/mockito-junit-5-extension中建议的所有步骤,如下所示:
依赖项:
代码:
public class Jupiter {
public boolean isAlpha() {
return true;
}
}
测试代码:
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import java.util.Date;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
public class JupiterTest {
@InjectMocks
private Jupiter jupiter;
@BeforeEach
public void setup() {
//usually some stuff here
}
@Test
@DisplayName("heading jupiter - make it so")
public void test() {
boolean result = jupiter.isAlpha();
assertThat(result).isTrue();
}
}
不幸的是,运行测试失败。
有人偶然发现类似的问题吗?是一般问题还是我的项目特定问题?
java.lang.NoSuchMethodError: org.junit.platform.commons.support.AnnotationSupport.findAnnotation(Ljava/util/Optional;Ljava/lang/Class;)Ljava/util/Optional;
at org.mockito.junit.jupiter.MockitoExtension.retrieveAnnotationFromTestClasses(MockitoExtension.java:178)
at org.mockito.junit.jupiter.MockitoExtension.beforeEach(MockitoExtension.java:160)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$null$0(TestMethodTestDescriptor.java:126)
...
Suppressed: java.lang.NullPointerException
at org.mockito.junit.jupiter.MockitoExtension.afterEach(MockitoExtension.java:214)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$null$11(TestMethodTestDescriptor.java:214)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:226)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:224)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachCallbacks(TestMethodTestDescriptor.java:213)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:116)
... 43 more
答案 0 :(得分:1)
不确定为什么需要所有老式的和平台的东西。.您的测试是org.junit.jupiter.api.Test;
我会这样做:
1)从班级删除所有注释
2)初始化Mockito:
@BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
}
3)这些部门应该足够了:
junit-jupiter 5.5.0
mockito-core 2.28.2
答案 1 :(得分:0)
mockito-junit-jupiter 2.28.2允许您使用这种方式,这可能可以解决您的问题。
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoSettings;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.quality.Strictness.LENIENT;
@MockitoSettings(strictness = LENIENT)
public class JupiterTest {
@InjectMocks
private Jupiter jupiter;
@BeforeEach
public void setup() {
//usually some stuff here
}
@Test
@DisplayName("heading jupiter - make it so")
public void test() {
boolean result = jupiter.isAlpha();
assertThat(result).isTrue();
}
}