我有一个非常简单的SpringBoot Junit,但是它一直失败。
@RunWith(SpringRunner.class)
//@SpringBootTest
@WebMvcTest(TokenServiceImpl.class)
public class TokenTest {
@Test
public void getOauthToken()
{
System.out.println( " done test");
}
我的TokenServiceImpl类具有
public class TokenServiceImpl implements TokenService{
public String getToken() throws RuntimeException{
return " Token returned" ;
}
我收到以下错误:-Snippet
2019-11-27 19:12:45.884警告15864 --- [主]o.s.w.c.s.GenericWebApplicationContext:遇到异常在上下文初始化期间-取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ runApplication”的bean时出错:不满意的依赖关系通过“工作”字段表示;嵌套异常为org.springframework.beans.factory.UnsatisfiedDependencyException:创建类路径资源中定义的名称为“ job”的bean时出错[com / mycompany / project1 / batch / configurations / BatchBDREntityConfig.class]:通过方法“作业”参数4表示的不满意依赖性;嵌套异常为org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ batchDBWriter”的bean时出错:不满意的依赖关系通过字段“ BDREntityRepository”表示;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:否类型的合格豆'com.mycompany.project1.batch.repositories.BDREntityRepository'可用:至少有1个符合自动装配条件的bean候选人。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
Field BDREntityRepository incom.mycompany.project1.batch.services.BatchDBWriter需要一个Bean键入“ com.mycompany.project1.batch.repositories.BDREntityRepository”找不到。
考虑定义类型的bean“ com.mycompany.project1.batch.repositories.BDREntityRepository”位于您的配置。
原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ runApplication”的bean时出错:不满意的依赖关系通过“工作”字段表示;嵌套异常为org.springframework.beans.factory.UnsatisfiedDependencyException:创建类路径资源中定义的名称为“ job”的bean时出错[com / mycompany / project1 / batch / configurations / BatchBDREntityConfig.class]:通过方法“作业”参数4表示的不满意依赖性;嵌套异常为org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ batchDBWriter”的bean时出错:不满意的依赖关系通过字段“ BDREntityRepository”表示;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:否类型的合格豆'com.mycompany.project1.batch.repositories.BDREntityRepository'可用:至少有1个符合自动装配条件的bean候选人。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“ batchDBWriter”的bean时出错:不满意的依赖关系通过字段“ BDREntityRepository”表示;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionException:否类型的合格豆'com.mycompany.project1.batch.repositories.BDREntityRepository'可用:至少有1个符合自动装配条件的bean候选人。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:否类型的合格豆'com.mycompany.project1.batch.repositories.BDREntityRepository'可用:至少有1个符合自动装配条件的bean候选人。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}
我的主要方法有以下内容
@SpringBootApplication
@ComponentScan(basePackages= {"com.mycompany.project1"})
public class RunApplication{
private static final Logger logger = Logger.getLogger(BatchController.class);
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
/*
* jmxBean will get loaded when this managed bean is called from RMI, and it will fire batch execution
*/
public static void main(String[] args){
ApplicationContext app = SpringApplication.run(RunApplication.class, args);
logger.debug("Batch Application has been started...");
BatchController controller = app.getBean(BatchController.class);
//Start batch process when application starts or restart, its optional call and can be commented out
//as we have JMX to expose load method on demand
//BatchController batch = new BatchController();
controller.startBatchProessFromMain();
}
}
@Component
public class BatchDBWriter implements ItemWriter<BDREntity> {
private static final Logger logger = Logger.getLogger(BatchDBWriter.class);
@Autowired
private BDREntityRepository bDREntityRepository;
/*
* this method call the JPArepository's saveAll
* function and saves all the list of bdrEntitiesat a time.
*/
@Override
public void write(List<? extends BDREntity> bdrEntities) throws Exception {
bDREntityRepository.saveAll(bdrEntities);
}
}
我该如何修复Junit?
您使用的是@WebMvcTest
,即test-slice annotation。它专门用于测试应用程序中与WebMvc相关的组件,并且是集成测试的一种形式。
您在评论中说过,您不是要对应用程序进行集成测试。在这种情况下,您不应使用@SpringBootTest
或Spring Boot提供的任何其他@…Test
注释。
假设您的目标是对TokenServiceImpl
进行单元测试,我希望您的测试类看起来像这样:
public class TokenTest {
private final TokenServiceImpl tokenService = new TokenServiceImpl();
@Test
public void getOauthToken() {
String token = this.tokenService.getToken();
// Assertions to check the token go here
}
}