Hello im new in testing and I first time trying to test a JpaRepository which interact with postgres in reality but in test it interacte with H2 Db i ve installed H2 dependencie and the software aswell but i still have a problem when using the repository on my test btw the dependencie is mention in scope test just to clarify more, any whenever i run this test below i got this error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field initService in com.project.socializer.SocializerApplication required a bean of type 'com.project.socializer.runner.InitService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.project.socializer.runner.InitService' in your configuration.
`***************************
APPLICATION FAILED TO START
***************************
Description:
Field initService in com.project.socializer.SocializerApplication required a bean of type 'com.project.socializer.runner.InitService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.project.socializer.runner.InitService' in your configuration.`
here is my code :
package com.project.socializer.user.repository;
import com.project.socializer.SocializerApplication;
import com.project.socializer.user.entity.Roles;
import com.project.socializer.user.entity.UserEntity;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest
class UserRepositoryTest {
@Autowired
UserRepository userRepository;
@BeforeEach
void setUp() {
userRepository.save(new UserEntity("Jhon","Doe","jhon.doe@gmail.com","test1234","1999-12-19",new Roles("USER")));
}
@AfterEach
void afterEach() {
userRepository.deleteAll();
}
@Test
void findByEmail() {
Optional<UserEntity> userTest = userRepository.findByEmail("jhon.doe@gmail.com");
assertTrue(userTest.isPresent(),"User Exist");
}
}
i tried many thing but sems nothing to work i dont know whats the problem and i hope for a fix from you.