Spring Boot Org. Junit. Runners. Model. Invalidtestclasserror: Invalid Test Class 1. Test Class Should Have Exactly One Public Zero-Argument Constructor

I have a problem about running some tests through JUnit in my Spring Boot application.

When I tried to run any test of repositorytest, I got the issue shown below.

org.junit.runners.model.InvalidTestClassError: Invalid test class 1. Test class should have exactly one public zero-argument constructor

I also defined @Runwith annotation but it didn't help me fix my issue.

import lombok.RequiredArgsConstructor;
import org.junit.Test;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

@DataJpaTest
@RequiredArgsConstructor
public class CategoryRepositoryTests {

    private final CategoryRepository categoryRepository;

    @Test
    public void givenCategoryObject_whenSave_thenReturnSavedCategory() {

        // given - precondition or setup
        Category category = Category.builder().name(CategoryType.COMIC.getValue()).build();

        // when -  action or the behaviour that we are going test
        Category savedCategory = categoryRepository.save(category);

        // then
        assertThat(savedCategory.getId()).isGreaterThan(0);
        assertThat(savedCategory).isNotNull();
    }
}

How can I fix it?

4

1 Answer

After I defined @RunWith(SpringRunner.class) , the issue disappeared.

Here is the solution shown below.

@DataJpaTest
@RunWith(SpringRunner.class)
public class CategoryRepositoryTests {

    @Autowired
    private CategoryRepository categoryRepository;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest