jeudi 22 août 2019

How to create a test in Springt Boot that calls a service wich includes constructor injection?

I'm trying to write a test in a Spring-Boot project. My problem is that I can't use my service that includes a constructor injection.

Depending on what I try I get errors like java.lang.IllegalStateException: Failed to load ApplicationContexts or NullPointerExceptions.

My first try was to change the constructor injection in my service to a field injection. But after reading this post I decided to change it back to the previous way. Then I searched for examples but couldn't find something that was helpful.

Following are the relevant code snippets. If more code is needed I would provide it.

The service class with the constructor injection:

PlayerServiceImpl.java

@Service
public class PlayerServiceImpl implements PlayerService {

    private PlayerRepository playerRepository;
    private CompanyService companyService;
    private CompanyResourceService companyResourceService;

    @Autowired
    public PlayerServiceImpl(PlayerRepository thePlayerRepository, CompanyService theCompanyService,
                             CompanyResourceService theCompanyResourceService) {
        this.playerRepository = thePlayerRepository;
        this.companyService = theCompanyService;
        this.companyResourceService = theCompanyResourceService;
    }

...

}

The test class im trying to create:

PlayerServiceImplIntegrationTest.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class PlayerServiceImplIntegrationTest {

    @TestConfiguration
    static class PlayerServiceImplTestContextConfiguration {

        private PlayerRepository playerRepository;
        private CompanyService companyService;
        private CompanyResourceService companyResourceService;

        @Bean
        public PlayerService playerService() {
            return new PlayerServiceImpl(playerRepository, companyService, companyResourceService);
        }
    }

    @Autowired
    private PlayerService playerService;

    @MockBean
    private PlayerRepository playerRepository;

    @Before
    public void setUp() {
        Player max = new Player("MaxMustang", "test123", "MaxMustang",
                "max.mustang@test.com", new Date(System.currentTimeMillis()), 1, 0,
                new BigDecimal("0.00"), new BigDecimal("0.00"), 0, 0);

        Mockito.when(playerRepository.findByUserName(max.getUserName()))
                .thenReturn(max);
    }

    @Test
    public void whenFindById_thenReturnPlayer() {
        String userName = "MaxMustang";

        Player found = playerService.findByUserName(userName);

        assertThat(found.getUserName()).isEqualTo(userName);
    }
}

In my test I'm trying to create a player object and receive it. It's just my first test in Spring Boot. And my main goal was to just get the test running. And the original test is from Baeldung from "5. Mocking with @MockBean". But while experimenting around, I added or changed a few things.

If I missed a post pointing at the same problem I would be glad to be informed about that.

Also I would appreciate it if someone can tell me if the arguments in the constructor of my service are to much or still in a "ok" range.

Aucun commentaire:

Enregistrer un commentaire