Reputation: 5769
I use spring boot 3
Main spring boot class
@EnableTransactionManagement
@SpringBootApplication
@Slf4j
public class FlexApplication{
private final ApplicationParameterManager appParamManager;
public FlexApplication(ApplicationParameterManager appParamManager) {
this.appParamManager = appParamManager;
}
@PostConstruct
public void init(){
}
....
}
@Service
@Slf4j
public class ApplicationParameterManager{
....
}
Basic test
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class ListUserRepositoryTest {
@Autowired
private ListUserRepository repository;
@Test
public void getListUserByUserType(){
String typeUser = "CETEST";
Pageable page = Pageable.ofSize(10);
Page<ListUser> pageListUser = repository.findAllByTypeUser(typeUser, page);
assertThat(pageListUser.getContent().size() > 5 ).isTrue();
}
}
Otherwise this test, application run well
I get this error
Parameter 0 of constructor in com.acme.FlexApplication required a bean of type 'com.acme.parameter.ApplicationParameterManager' that could not be found.
Upvotes: 0
Views: 530
Reputation: 648
When using @DataJpaTest, you are not creating the whole spring context as when you run the application normally but you only create the beans responsible for data access layer.
In order to run your tests properly, you need to provide a mocked bean of type ApplicationParameterManager
.
The easiest way to do it is by utilizing @MockBean annotation
.
So, to make your tests work, edit the test in the following way.
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class ListUserRepositoryTest {
@MockBean
private ApplicationParameterManager applicationParameterManager;
@Autowired
private ListUserRepository repository;
@Test
public void getListUserByUserType(){
String typeUser = "CETEST";
Pageable page = Pageable.ofSize(10);
Page<ListUser> pageListUser = repository.findAllByTypeUser(typeUser, page);
assertThat(pageListUser.getContent().size() > 5 ).isTrue();
}
}
That way, the spring context will include a mocked bean of your required dependency.
Take a look at @MockBean
java doc for more information. https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/mock/mockito/MockBean.html
If you prefer to run the whole spring context in order to perform full integration tests, take a look at @SpringBootTest
annotation.
@DataJpaTest
should be used when you want to test data access layer in isolation.
Upvotes: 1
Reputation: 4281
I think it is not related to version of Spring Boot.
As you're using @DataJpaTest
, your bean is not created
@DataJpaTest can be used if you want to test JPA applications. By default it will configure an in-memory embedded database, scan for @Entity classes and configure Spring Data JPA repositories. Regular @Component beans will not be loaded into the ApplicationContext.
Solution would be to use @SpringBootTest
instead of @DataJpaTest
if your test is not really a JPA test.
Also, still using @DataJpaTest
you could add @Import(ApplicationParameterManager.class)
to your test class
Upvotes: 1