在 spring 的测试单元中注入 @PersistenceContext
Inject @PersistenceContext in test unit for spring
我在测试中注入 entityManager 时遇到问题 class。我认为这是因为我无法在我的测试 class.
中加载我的 spring 上下文
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
这是我的测试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()
{
}
}
这是测试的配置我知道当我使用 new 时不会在 dao 中注入 entitiesManager define 但我不知道我应该做什么。
@Configuration
public class NoteDeFraisTestConfig {
@Bean
NoteDeFraisDAO noteDeFraisDAO()
{
return new NoteDeFraisDAO();
}
}
我试图将我的 ContextConfiguration 设置为我的 applicationContext 但它没有用我认为这是因为目录 WEB-INF 不属于 class 路径。我该如何解决这个问题??
这是我项目的结构
谢谢。
更新
这是我的期末考试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()
{
}
}
是的,问题是为您的测试加载的 ApplicationContext
不包含 LocalContainerEntityManagerFactoryBean
。假设在 applicationContext.xml
中正确声明,下面的链接解决方案将有所帮助。
I have tried to set my ContextConfiguration to my applicationContext but it didn't work I think that it is because the directory WEB-INF doesn't belong to the classpath. How can I fix this??
此处已涵盖:Location of spring-context.xml
我在测试中注入 entityManager 时遇到问题 class。我认为这是因为我无法在我的测试 class.
中加载我的 spring 上下文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
这是我的测试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()
{
}
}
这是测试的配置我知道当我使用 new 时不会在 dao 中注入 entitiesManager define 但我不知道我应该做什么。
@Configuration
public class NoteDeFraisTestConfig {
@Bean
NoteDeFraisDAO noteDeFraisDAO()
{
return new NoteDeFraisDAO();
}
}
我试图将我的 ContextConfiguration 设置为我的 applicationContext 但它没有用我认为这是因为目录 WEB-INF 不属于 class 路径。我该如何解决这个问题??
这是我项目的结构
谢谢。
更新
这是我的期末考试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()
{
}
}
是的,问题是为您的测试加载的 ApplicationContext
不包含 LocalContainerEntityManagerFactoryBean
。假设在 applicationContext.xml
中正确声明,下面的链接解决方案将有所帮助。
I have tried to set my ContextConfiguration to my applicationContext but it didn't work I think that it is because the directory WEB-INF doesn't belong to the classpath. How can I fix this??
此处已涵盖:Location of spring-context.xml