Inject @PersistenceContext in test block for spring

I have a problem injecting entityManager into my test class. I think it has something to do with the fact that I cannot load the spring context into my test class.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'noteDeFraisDAO': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available

      

this is my test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {NoteDeFraisTestConfig.class})
@Transactional

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class NoteDeFraisDAOIT
{
    @Autowired
    private NoteDeFraisDAO noteDeFraisDAO;

    @Test
    public void myTest()
    {

    }

}

      

And this is the configuration for the test I know that when I use the new one, the entityManager definition will not be injected into the dao, but I don't know what to do.

@Configuration
public class NoteDeFraisTestConfig {

    @Bean
    NoteDeFraisDAO noteDeFraisDAO()
    {
        return new NoteDeFraisDAO();
    }
}

      

I tried to set the ContextConfiguration to my applicationContext, but it didn't work. I think this is because the WEB-INF directory does not belong to the classpath. How can I fix this?

This is my project structure enter image description here

thank.

Update

this is my last test class

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:web/WEB-INF/applicationContext.xml", "file:web/WEB-INF/db-config.xml","file:web/WEB-INF/dispatcher-servlet.xml","file:web/WEB-INF/security-config.xml"})
@Transactional
public class NoteDeFraisDAOIT
{
    @Autowired
    private NoteDeFraisDAO noteDeFraisDAO;

    @Test
    public void myTest()
    {

    }

}

      

+3


source to share


1 answer


Yes, the problem is that the one downloaded ApplicationContext

for your test does not contain LocalContainerEntityManagerFactoryBean

. Assuming it's stated correctly in the applicationContext.xml

linked solution below will help.

I tried to set the ContextConfiguration to my applicationContext, but it didn't work. I think this is because the WEB-INF directory does not belong to the classpath. How can I fix this?



Here: Location of spring-context.xml

+1


source







All Articles