teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

エラー改善

2021/05/02 20:42

投稿

goriwo
goriwo

スコア17

title CHANGED
File without changes
body CHANGED
@@ -57,6 +57,10 @@
57
57
  ### 試したこと
58
58
 
59
59
  ビルドパスの構成でやってみたんですがいまいちわかりませんでした
60
+ ```
61
+ @Sql("testdata.sql")
62
+ ```
63
+ この指定の仕方でもだめでした
60
64
  今のファイルの配置
61
65
  ![イメージ説明](4b6f18d70063d33774854f95a91498c3.png)
62
66
 

3

エラー改善

2021/05/02 20:42

投稿

goriwo
goriwo

スコア17

title CHANGED
File without changes
body CHANGED
File without changes

2

エラー改善

2021/05/02 20:39

投稿

goriwo
goriwo

スコア17

title CHANGED
File without changes
body CHANGED
@@ -57,5 +57,7 @@
57
57
  ### 試したこと
58
58
 
59
59
  ビルドパスの構成でやってみたんですがいまいちわかりませんでした
60
+ 今のファイルの配置
61
+ ![イメージ説明](4b6f18d70063d33774854f95a91498c3.png)
60
62
 
61
63
  ### 補足情報(FW/ツールのバージョンなど)

1

エラー改善

2021/05/02 20:36

投稿

goriwo
goriwo

スコア17

title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,52 @@
8
8
  /resources
9
9
  このtestを分けて上のようにしたいです
10
10
 
11
+ エラー文
12
+ ```
13
+ : Rolled back transaction for test: [DefaultTestContext@2bb7bd00 testClass = UserDaoTest, testInstance = com.example.demo.UserDaoTest@5358c8fa, testMethod = countTest2@UserDaoTest, testException = org.springframework.jdbc.datasource.init.CannotReadScriptException: Cannot read SQL script from class path resource [src/test/resources/testdata.sql]; nested exception is java.io.FileNotFoundException: class path resource [src/test/resources/testdata.sql] cannot be opened because it does not exist, mergedContextConfiguration = [WebMergedContextConfiguration@5f031ebd testClass = UserDaoTest, locations = '{}', classes = '{class com.example.demo.SpringSampleApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.t
14
+ ```
11
15
 
16
+ ソースコード
17
+ ```
18
+ package com.example.demo;
19
+
20
+ import static org.junit.jupiter.api.Assertions.*;
21
+
22
+ import org.junit.jupiter.api.Test;
23
+ import org.junit.runner.RunWith;
24
+ import org.springframework.beans.factory.annotation.Autowired;
25
+ import org.springframework.beans.factory.annotation.Qualifier;
26
+ import org.springframework.boot.test.context.SpringBootTest;
27
+ import org.springframework.test.context.jdbc.Sql;
28
+ import org.springframework.test.context.junit4.SpringRunner;
29
+ import org.springframework.transaction.annotation.Transactional;
30
+
31
+ import com.example.demo.Repository.UserDao;
32
+ //JUnit Javaプログラムの単体テストを行うためのツール
33
+ //テスト用のアノテーション
34
+ @RunWith(SpringRunner.class)//テストをどのクラスで実行するか指定(spring用のJUnitを使えるクラス)
35
+ @SpringBootTest//springbootを起動してからテスト開始
36
+ @Transactional//トランザクションの開始、コミット、ロールバックは自動
37
+ public class UserDaoTest {
38
+ @Autowired
39
+ @Qualifier("UserDaoJdbcImpl")
40
+ UserDao dao;
41
+ //カウントメソッドのテスト1
42
+ @Test
43
+ public void countTest1() {
44
+ //カウントメソッドの結果が2件であることをテスト
45
+ assertEquals(dao.count(),2);
46
+ }
47
+ @Test
48
+ @Sql(scripts="classpath:src/test/resources/testdata.sql")//SQLを実行した後の状態でテスト
49
+ public void countTest2() {
50
+ //カウントメソッドの結果が3件であることをテスト
51
+ assertEquals(dao.count(),3);
52
+ }
53
+
54
+ }
55
+
56
+ ```
12
57
  ### 試したこと
13
58
 
14
59
  ビルドパスの構成でやってみたんですがいまいちわかりませんでした