【发布时间】:2016-05-28 21:40:22
【问题描述】:
我是弹簧靴的新手。需要一些建议 这是我的单元测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
public class EmployeeRepositoryTest {
@Autowired
protected EmployeeRepository employeeRepository;
@Test
public void insertEmploee(){
Employee employee = new Employee();
employee.setEmpName("Azad");
employee.setEmpDesignation("Engg");
employee.setEmpSalary(12.5f);
employeeRepository.save(employee);
}
}
当我运行它时,我得到异常
java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotationAttributes(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/String;ZZ)Lorg/springframework/core/annotation/AnnotationAttributes;
at org.springframework.test.util.MetaAnnotationUtils$AnnotationDescriptor.<init>(MetaAnnotationUtils.java:290)
at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:365)
at org.springframework.test.util.MetaAnnotationUtils$UntypedAnnotationDescriptor.<init>(MetaAnnotationUtils.java:360)
at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:191)
at org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes(MetaAnnotationUtils.java:198)
at
进程以退出代码 -1 结束
【问题讨论】:
-
看起来你的依赖有问题(也许你正在混合一些 Spring-Versions)。您能否提供您的依赖项(如果您使用的是 maven 或等效的 gradle 文件,则为 pom.xml)?
-
@MikeBoddin 已解决。你说得对
-
顺便说一句,如果您在测试中启动 Spring 上下文,它通常不再被视为单元测试。
-
@luboskrnac 我有兴趣了解更多详情
-
只是命名/标题是错误的,这更像是一个集成测试而不是实际的单元测试。对于单元测试(当您只需要测试一些代码时),使用
SpringJUnit4ClassRunner运行器可能不是一个好主意,因为它启动整个应用程序并且比不使用运行器慢很多。在这种情况下,您可能会检查是否在数据库中创建了记录,在这种情况下,您正在编写集成测试。
标签: java junit spring-boot