I'm having an issue trying to make test in my spring-boot project.
as you can see, my project is devided with maven modules. "alta-launcher" is the "main" project getting every other module as dependencies. The problem is that my tests in the user module won't get the spring context so my fields "Autowired" will be null on runtime.
Any ideas how to configure this project to be able to do tests in each modules ?
Edit :
@SpringBootTest(classes = CoreApplication.class)
class UserQueryTransformerImplTest {
@Autowired
private UserQueryTransformer userQueryTransformer;
@Test
void toDTO() {
UserEntity userEntity = new UserEntity();
userEntity.setLogin("everest");
userEntity.setFirstName("Everest");
userEntity.setLastName("Mountain");
UserQueryDTO userQueryDTO = userQueryTransformer.toDTO(userEntity);
assertEquals(userEntity.getLogin(), userQueryDTO.getLogin());
assertEquals(userEntity.getFirstName(), userQueryDTO.getFirstName());
assertEquals(userEntity.getLastName(), userQueryDTO.getLastName());
}
The annotation @SpringBootTest with the attribute classes is unusable because I don't have access to the launcher module from user module. And without the attribute (juste @SprinBootTest) is when I'm getting my autowire field null which makes sense cause I don't have the context.
I was able to fix the issue by using ContextConfiguration annotation. (ExtendWith is for the junit5 part)
@ContextConfiguration(classes = ConfigurationTest.class)
@ExtendWith(SpringExtension.class)
class UserQueryTransformerImplTest {
@Autowired
private UserQueryTransformer userQueryTransformer;
@Test
void toDTO() {
UserEntity userEntity = new UserEntity();
userEntity.setLogin("everest");
userEntity.setFirstName("Everest");
userEntity.setLastName("Mountain");
UserQueryDTO userQueryDTO = userQueryTransformer.toDTO(userEntity);
assertEquals(userEntity.getLogin(), userQueryDTO.getLogin());
assertEquals(userEntity.getFirstName(), userQueryDTO.getFirstName());
assertEquals(userEntity.getLastName(), userQueryDTO.getLastName());
}
and here is my configurationTest.java
@TestConfiguration
public class ConfigurationTest {
@Bean
UserQueryTransformer createUserQueryTransformer() {
return new UserQueryTransformerImpl();
}
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
UserQueryDTO createUserQueryDTO() {
return new UserQueryDTO();
}