Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing a Spring Boot service class with(out) repository in JUnit
    primarykey
    data
    text
    <p>I am working on a spring boot based webservice with following structure:</p> <p>Controller (REST) --> Services --> Repositories (as suggested in some tutorials).</p> <p>My Database Connection (JPA/Hibernate/MySQL) is defined in a @Configuration class. (see below)</p> <p>Now I'd like to write simple tests for methods in my Service classes, but I don't really understand how to load ApplicationContext into my test classes and how to mock the JPA / Repositories. </p> <p>This is how far I came:</p> <p>My service class</p> <pre><code>@Component public class SessionService { @Autowired private SessionRepository sessionRepository; public void MethodIWantToTest(int i){ }; [...] } </code></pre> <p>My test class:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = AnnotationConfigContextLoader.class) public class SessionServiceTest { @Configuration static class ContextConfiguration { @Bean public SessionService sessionService() { return new SessionService(); } } @Autowired SessionService sessionService; @Test public void testMethod(){ [...] } } </code></pre> <p>But I get following exception:</p> <blockquote> <p>Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.myApp.SessionRepository com.myApp.SessionService.sessionRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myApp.SessionRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}</p> </blockquote> <p>For completeness: here's my @Configuration for jpa:</p> <pre><code>@Configuration @EnableJpaRepositories(basePackages={"com.myApp.repositories"}) @EnableTransactionManagement public class JpaConfig { @Bean public ComboPooledDataSource dataSource() throws PropertyVetoException, IOException { ... } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { ... } @Bean public JpaVendorAdapter jpaVendorAdapter() { ... } @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { ... } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){ ... } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload