前提・実現したいこと
SpringBootでバッチアプリケーションを作成し、現在JUnitテストを書いています。
テスト対象のクラスに@Autowiredがあるため、
テストクラスに@SpringBootTestをつけて実行したのですが、エラーが発生してしまいます。
いろいろと調べては試してみているのですが、解決せず…
何かお気づきの点がありましたら、ご教示いただけますと大変ありがたいです。
発生している問題・エラーメッセージ
12:04:19.047 [main] WARN o.s.c.a.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'batchController' defined in file []: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} 12:04:19.190 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - *************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in jp.co.controller.BatchController required a bean of type 'java.lang.String' that could not be found. Action: Consider defining a bean of type 'java.lang.String' in your configuration. 12:04:19.196 [main] ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@1218e12] to prepare test instance [SampleDaoTest@xxxx] java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227) …
該当のソースコード
BatchApplication.java
java
1@SpringBootApplication 2public class BatchApplication { 3 4 public static void main(String[] args) { 5 try { 6 SpringApplication application = new SpringApplication(BatchApplication.class); 7 application.run(args); 8 } catch (Exception e) { 9 } 10 } 11}
BatchController.java
java
1@Configuration 2public class BatchController implements ApplicationRunner { 3 4 public BatchController(String profileName) { 5 try { 6 LoggerConfigLoader.loadLogbackConfig(profileName); 7 } catch (ApplicationException e) { 8 } 9 } 10 11 @Profile("test") 12 public static class TestConfig { 13 @Bean 14 @ConditionalOnProperty(value = { "batch.execute" }, havingValue = "BatchController") 15 public BatchController batchController() { 16 return new BatchController("test"); 17 } 18 } 19 20 @Override 21 public void run(ApplicationArguments args) throws ApplicationException { 22 // 処理 23 } 24}
DataConfig.java
java
1@Configuration 2@MapperScan("jp.co.dao") 3public class DataConfig { 4 5 @Bean 6 public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException { 7 8 SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); 9 factory.setDataSource(dataSource); 10 ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader()); 11 factory.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml")); 12 factory.setMapperLocations(resolver.getResources("classpath:sql/*.xml")); 13 14 return factory; 15 } 16}
SampleDao.java(テスト対象クラス)
java
1@Component 2public class SampleDao { 3 4 @Autowired 5 private SampleMapper mapper; 6 7 public int update(SampleEntity entity){ 8 return mapper.updateByPrimaryKeySelective(entity); 9 } 10}
SampleDaoTest.java(テストクラス)
java
1@RunWith(SpringRunner.class) 2@SpringBootTest 3@ActiveProfiles("test") 4public class SampleDaoTest { 5 6 @Autowired 7 private SampleDao dao; 8 9 @Test 10 public void testUpdate() { 11 12 SampleEntity entity = new SampleEntity(); 13 entity.setId("1234567"); 14 15 int result = dao.update(entity); 16 // 以下に検証するコード 17 } 18}
pom.xml
xml
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>2.2.1.RELEASE</version> 10 <relativePath/> 11 </parent> 12 13 <properties> 14 <java.version>1.8</java.version> 15 </properties> 16 17 <dependencies> 18 <dependency> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter</artifactId> 21 </dependency> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-configuration-processor</artifactId> 25 <optional>true</optional> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-test</artifactId> 30 <scope>test</scope> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-devtools</artifactId> 35 <scope>runtime</scope> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-jdbc</artifactId> 40 </dependency> 41 <dependency> 42 <groupId>org.springframework.boot</groupId> 43 <artifactId>spring-boot-starter-aop</artifactId> 44 </dependency> 45 46 <dependency> 47 <groupId>org.postgresql</groupId> 48 <artifactId>postgresql</artifactId> 49 <scope>runtime</scope> 50 </dependency> 51 <dependency> 52 <groupId>org.mybatis</groupId> 53 <artifactId>mybatis</artifactId> 54 <version>3.4.6</version> 55 </dependency> 56 <dependency> 57 <groupId>org.mybatis</groupId> 58 <artifactId>mybatis-spring</artifactId> 59 <version>1.3.2</version> 60 </dependency> 61</project>
補足情報
NetBeans使用です。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/22 04:51