私はSpringブートRestコントローラのテストを書いています。この残りのコントローラは、いくつかの値をdbに書き込みます。メモリ内のDBを使用してRestコントローラのテストを記述する
このテストにSpringが提供するインメモリデータベースを使用したいと思います。さらに下、私は次の例外がスローされた参照エラースタックトレースで
java.lang.IllegalStateException: Failed to load ApplicationContext
:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Failed to replace DataSource with an embedded database for tests. If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoconfigureTestDatabase.
このthis docによると、私はこのエラーが発生し@DataJpaTest
とテストクラスを、注釈を付ける必要があり私が取り組んでいるテストクラスです:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@DataJpaTest
public class AuthenticationControllerFTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private AuthenticationManager authenticationManager;
@Autowired
private WebApplicationContext context;
@Autowired
private Filter springSecurityFilterChain;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.addFilters(springSecurityFilterChain).build();
}
@Test
public void testCreate() throws Exception {
String exampleUserInfo = "{\"name\":\"Salam12333\",\"username\":\"[email protected]\",\"password\":\"Salam12345\"}";
RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/signup")
.accept(MediaType.APPLICATION_JSON).content(exampleUserInfo)
.contentType(MediaType.APPLICATION_JSON);
MvcResult result = mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse response = result.getResponse();
int status = response.getStatus();
Assert.assertEquals("http response status is wrong", 200, status);
}
}
このエラーの原因は何ですか?
編集1 これは私のapplication.properties
の内容です:
spring.datasource.username = hello
spring.datasource.password = hello
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/myproject?useSSL=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
logging.level.org.springframework.web=DEBUG
server.port = 8443
server.ssl.key-store = classpath:tomcat.keystore
server.ssl.key-store-password = hello
server.ssl.key-password = hello
server.ssl.enabled = true
server.ssl.key-alias=myproject
編集2
私はpom.xml
に次を追加しました:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
私はapplication-test.properties
を作成ウィ以下の内容番目:
spring.datasource.username= root
spring.datasource.password= password
spring.datasource.driver-class-name= org.h2.Driver
spring.datasource.url= jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
- ユーザ名とpassowrdは何ですか?どこに置いたらいいですか?
- 私はこの行を含むエラーを取得し、テストを実行したときに、私はまた、テストクラスに
@ActiveProfiles("test")
を追加しました:
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
"組み込みデータベースが必要な場合は、サポートされているクラスパスをクラスパスに追加するか、@AutoconfigureTestDatabaseの置換属性を調整してください。だから、あなたはクラスパスに組み込みのデータベースを入れましたか? –
私の 'application.properties'ファイルの内容を元の投稿に追加します。 –
application.propertiesはクラスパスに影響しません。 pom.xmlにh2またはhsqldbを追加する必要があります。 –