Reputation: 213
I'm working on a Spring Boot project right now.
I want to test custom query inside an interface with extends the JpaRepository
. Here's the code that I can come up with :
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase
public class CffRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private CffRepository cffRepository;
@Test
public void saveCffTest() {
}
}
The problem is that for CffRepository
can't be autowired
because No Beans of it was found. I think it's because the CffRepository
is in different module with the MainApplication. Here's my project structure :
Can someone help me with this problem?
Upvotes: 3
Views: 1616
Reputation: 890
As you mentioned the problem is that you don't have a class annotated with @SpringBootApplication
in the classpath so the test doesn't know where to get the Spring configuration from.
One simple thing you can do is to include a TestApplication class in your /test
dir:
@SpringBootApplication()
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
As a bonus tip you don't actually need to enable auto-configuration for all the features you are using in production. You can get away with just using the parts required for your tests.
Upvotes: 2