提问人:Ousmane MBINTE 提问时间:11/10/2023 最后编辑:Ousmane MBINTE 更新时间:11/13/2023 访问量:54
Freemarker Junit 测试中没有可用的 'javax.persistence.EntityManagerFactory' 类型的合格 Bean
No qualifying bean of type 'javax.persistence.EntityManagerFactory' available in Freemarker Junit test
问:
我尝试使用 Spring 和 Freemarker 模板解决方案生成 pdf 文档。当我尝试使用Junit测试运行文档生成时,我有一个关于上下文加载的错误,说:
没有“javax.persistence.EntityManagerFactory”类型的限定 Bean 可用
我的项目不使用 Spring Boot,而只使用 Spring Core 和 Spring MVC。任何人都可以帮助我解决我的问题,因为我被卡住了。
Freemarker 配置 :
package com.omb;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.omb"})
public class FreeMarkerConfiguration implements WebMvcConfigurer {
@Bean
public FreeMarkerViewResolver freemarkerViewResolver() {
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
resolver.setCache(true);
resolver.setPrefix("");
resolver.setSuffix(".ftl");
return resolver;
}
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("/WEB-INF/freemarker");
return configurer;
}
}
Junit 测试
package com.omb.test;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= FreeMarkerConfiguration.class)
@WebAppConfiguration
public class DocGeneratorTest {
@Test
public void testGenerateDoc() throws IOException, TemplateException {
Map params = Map.of(PAGE_SIZE, PRINTER);
Map<String, Object> map = new HashMap<>();
byte[] content = DocGeneratorUtils.generateDoc("TEMPLATE_NAME", Files.readString(Path.of("./src/test/resources/model/template.html")), map, params);
try (FileOutputStream fos = new FileOutputStream(name + ".pdf")) {
fos.write(content);
}
}
}
叠:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1221)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:565)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:528)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:696)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:669)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:228)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessProperties(PersistenceAnnotationBeanPostProcessor.java:348)
... 43 more
答: 暂无答案
评论