0

I'm using Spring 2.4 and jUnit 5.7.

I'm trying to use a repository create for test but I'm getting "No qualifying bean of type".

I annotated the repository @TestComponent.

package com.cjgmj.dynamicQuery.persistence.repository;

import org.springframework.boot.test.context.TestComponent;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.cjgmj.dynamicQuery.persistence.entity.DummyEntity;

@TestComponent
public interface DummyRepository extends JpaSpecificationExecutor<DummyEntity> {

}

And I'm trying to get it on my Test class.

@SpringBootTest
public class TextLikeSpecificationTests {

    @Autowired
    private DummyRepository dummyRepository;

    ...

}

The project structure is:

- src/test/java  
|- projectPackage  
  |- persistence
    |- entity
      |- DummyEntity
    |- repository
      |- DummyRepository

I tried place the repository and the entity on main package, and also without annotation in the repository, but I'd got the same error.What I'm doing wrong?

Thank you in advance.

cjgmj
  • 152
  • 1
  • 2
  • 10
  • you should use Mockito and return dummy object. Autowiring is not a good idea. – priyranjan Nov 19 '20 at 16:01
  • but I need to execute the SQL to try that the specification is what I spect. – cjgmj Nov 19 '20 at 16:05
  • @priyranjan This is not true! the author wants to get an integration test. What is the point of an integration test when you mock a repository? – misnomer42 Nov 19 '20 at 16:05
  • where you have mentioned integration testing? did you check? how ppl will come to know? refer this link it might help you https://www.programmersought.com/article/51444661474/ – priyranjan Nov 19 '20 at 16:14
  • The problem isn't how to use JpaSpecificationExecutor, the problem is how to inject the repository. I tried with JpaRepository and It was injected sucessfully and it have the same implementation than JpaSpecificationExecutor. – cjgmj Nov 19 '20 at 16:29

1 Answers1

0

I just solved this problem. The first step was change the repository to extends JpaRepositoryImplementation. Then it throws a UnsupportedOperationException. Diging a bit I reach this post and I changed all the Collections.emptyList and Arrays.asList() to instanciate it as new ArrayList<>(). Then the test run properly.

cjgmj
  • 152
  • 1
  • 2
  • 10