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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.