Spring boot Rest denetleyicisi için bir test yapıyorum. Bu dinlenme denetleyicisi bazı değerleri db'ye yazar.Rest denetleyicileri için test yazmak için in-memory db kullanın
Bu test için Spring'in sağladığı bellek içi veritabanını kullanmak istiyorum.
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.
Bu: daha aşağı Aşağıdaki özel durum bkz hata yığın izleme
java.lang.IllegalStateException: Failed to load ApplicationContext
: this doc göre bu hataya neden olur @DataJpaTest
, test sınıfı açıklama zorunda Üzerinde çalıştığım test sınıfı:
@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);
}
}
Bu hata neden oluyor?
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
Düzenleme 2
benim pom.xml
için aşağıdaki eklendi:
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
Ben application-test.properties
oluşturulan düzenleme 1 Bu
benim application.properties
içeriğidir wi
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
- kullanıcı adı ve passowrd nedir: Aşağıdaki içerik th? Onları nerede ayarlamalıyım?
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
"Gömülü bir veritabanı istiyorsanız, lütfen desteklenen bir dosyayı sınıf yoluna koyun veya @OtomatikConfigureTestDatabase'in Değiştir a1> özniteliğini ayarlayın. Yani, sınıf yoluna gömülü bir veritabanı koydun mu? –
'application.properties' dosya içeriğimi asıl gönderiye ekliyorum. –
application.properties, sınıf yolunu etkilemez. Pom.xml dosyanıza h2 veya hsqldb eklemeniz gerekir. –