1

I'm new in Spring. I am creating a console application using Spring Boot. The application works fine, but when I try to test the Repository, I get an error

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'universityManagerSpringBootApplication': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.universitymanager.springboot.engine.ResponseGenerator' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Please help

ResponseGenerator

@Component
@AllArgsConstructor
public class ResponseGenerator {

    private final DepartmentsRepository departmentsRepository;
    private final LectorsRepository lectorsRepository;
...

Departments

@Entity
@NoArgsConstructor
@Setter
@Getter
@ToString
@Table(name = "departments")
public class Departments {
...

Lectors

@Entity
@NoArgsConstructor
@Setter
@Getter
@ToString
@Table(name = "lectors")
public class Lectors {
...

DepartmentsRepository

@Repository
public interface DepartmentsRepository extends CrudRepository<Departments, Integer> {
...

LectorsRepository

@Repository
public interface LectorsRepository extends CrudRepository<Lectors, Integer> {
...

UniversityManagerSpringBootApplication

@SpringBootApplication
@AllArgsConstructor
public class UniversityManagerSpringBootApplication implements CommandLineRunner {

    private ResponseGenerator responseGenerator;

    public static void main(String[] args) {
        SpringApplication.run(UniversityManagerSpringBootApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
...

DepartmentsRepositoryTest (caused error)

@DataJpaTest
class DepartmentsRepositoryTest {

    @Autowired
    private DepartmentsRepository departmentsRepository;

    @Test
    void injectedComponentIsNotNull(){
        assertNotNull(departmentsRepository);
    }
...
blast0ver
  • 31
  • 1
  • 5

3 Answers3

2

Please try to change @DataJpaTest to @SpringBootTest in your test to make Spring set up at full application context including a ResponseGenerator bean.

The documentation for @DataJpaTest says:

Using this annotation will disable full auto-configuration and instead apply only configuration relevant to JPA tests.

So with this annotation you don't get a ResponseGenerator bean which the stacktrace referes to.

Kaj Hejer
  • 955
  • 4
  • 18
  • 1
    He shouldn't need it. The problem is that the `@SpringBootApplication` implements `CommandLineRunner` and depends on that bean. That bean won't be available for a JPA only test. Basically with this setup the ability to use the specialized sliced tests won't work anymore. So the better advice would be to change the setup of his application instead of leading him to a full blown integration test. – M. Deinum Aug 07 '21 at 17:33
  • @Kaj Hejer With annotation `@SpringBootTest` the test hangs and cannot be completed – blast0ver Aug 08 '21 at 09:35
  • @M. Deinum Yes, you're right. Without the `CommandLineRunner` implementation, the test works properly, but the program can't perform its functions. What would you advise me to change to make the program run in console mode and the tests work? – blast0ver Aug 08 '21 at 09:42
  • 1
    Make it a separate class that will be detected and run. This detection will be disabled whilst running the test. It might also work as an `@Bean` method and implemented as a lamda (but I'm not 100% sure). – M. Deinum Aug 08 '21 at 17:28
2

I found a solution to my problem! This works fine for me https://stackoverflow.com/a/29774201/15349979

Glad if this helps someone

blast0ver
  • 31
  • 1
  • 5
1

If you are using TestNG, try to have your test class extends AbstractTestNGSpringContextTests which helps to access spring components in TestNG.

public class DepartmentsRepositoryTest extends AbstractTestNGSpringContextTests