質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
JUnit

JUnitは、Javaで開発されたプログラムのユニットテストを行うためのアプリケーションフレームワークです。簡単にプログラムのユニットテストを自動化することができ、結果もわかりやすく表示されるため効率的に開発時間を短縮できます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

1回答

26947閲覧

SpringBootのJUnitで"Failed to load ApplicationContext"エラーが発生する

yyskmr

総合スコア4

JUnit

JUnitは、Javaで開発されたプログラムのユニットテストを行うためのアプリケーションフレームワークです。簡単にプログラムのユニットテストを自動化することができ、結果もわかりやすく表示されるため効率的に開発時間を短縮できます。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2019/11/22 04:27

編集2022/01/12 10:55

前提・実現したいこと

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使用です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

テスト対象クラスとは無関係なクラス

java

1jp.co.controller.BatchController

にてSpringが対象クラスをインスタンス化した際に失敗しています。

required a bean of type 'java.lang.String' that could not be found.

とあるように、このControllerに何か余計な実装は残っていませんでしょうか。

投稿2019/11/22 04:41

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yyskmr

2019/11/22 04:51

ご返信&ご指摘ありがとうございます! BatchController.javaを記載し忘れていたので追記しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問