首页 文章

在Travis CI,Spring Boot和PostgreSQL中运行时出现EntityManagerException

提问于
浏览
1

我使用Spring Boot,PostgreSQL,Maven和JUnit创建了一个简单的Web应用程序 . 在我的IDE中运行它(mvn clear-verify)一切都很完美但是在Travis CI中运行时遇到了这个异常:

在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]中定义名称为'entityManagerFactory'的bean创建错误:init方法的调用失败;嵌套异常是javax.persistence.PersistenceException:[PersistenceUnit:default]无法构建Hibernate SessionFactory

还有很多其他人 . 我的测试在IDE中也运行得很好 . 有人可以告诉我为什么吗?我的代码在这里:

实体:

@Entity
@Table(name = "contacts")
public class Contact extends BaseEntity{

    @Column(name = "name", nullable = false)
    private String name;

    public Contact() {
    }
    public Contact(String name) {
        this(null,name);
    }
    public Contact(Integer id, String name) {
        super(id);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Contact{" +
                "id='" + id + '\'' +
                "name='" + name + '\'' +
                '}';
    }
}

库:

@Repository
public class ContactRepositoryImpl implements ContactRepository{

    private static final Logger LOG = LoggerFactory.getLogger(ContactRepositoryImpl.class);

    @Autowired
    private ProxyContactRepository proxyContactRepository;

    private Pattern regexPattern;

    @Override
    public List<Contact> getAllSorted(String nameFilter) {
        List<Contact> listOfAllContacts = new CopyOnWriteArrayList<>();
        try {
            regexPattern = Pattern.compile(nameFilter);
            if (!nameFilter.isEmpty() || nameFilter.length() != 0) {
                listOfAllContacts.addAll(getAll().stream().filter(contact -> notDoMatch(contact.getName())).collect(Collectors.toList()));
            } else {
                LOG.warn("Regex parameter " + "'" + nameFilter + "'" + " is empty");
                throw new NotFoundException("Regex parameter is empty");
            }
            return listOfAllContacts;
        }
        catch (PatternSyntaxException exception){
            LOG.error("Regex parameter " + "'" + nameFilter + "'" + " is invalid");
            throw new NotFoundException("Regex parameter is invalid");
        }
    }

    @Override
    public List<Contact> getAll() {
        return proxyContactRepository.findAll();
    }

    private boolean notDoMatch(String word){
        Matcher matcher = regexPattern.matcher(word);
        return !matcher.matches();
    }
}

控制器:

@RestController
@RequestMapping("/contacts")
public class ContactController extends AbstractContactController{

    @RequestMapping(method = RequestMethod.GET, params = "nameFilter")
    public List<Contact> getSortedPage(@RequestParam("nameFilter") String nameFilter){
        return super.getAllSorted(nameFilter);
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<Contact> getAllPage(){
        return super.getAll();
    }
}

travis.yml

language: java
script: mvn clean verify
jdk: oraclejdk8

services:
- postgresql

before_script:
- psql -c 'create database hello;' -U postgres

和app.properties:

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/hello
spring.datasource.username=postgres
spring.datasource.password=password

spring.jpa.hibernate.ddl-auto=validate
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL81Dialect

enter image description here
我检查了与PostgreSQL DB的连接并且它可以工作(你可以在图片上看到它),但是我没有在Test中使用Entity Manager . 有人可以告诉我它是什么吗?

1 回答

  • 1

    我建议使用H2进行集成测试而不是PostgreSQL . 您无需设置任何TravisCI服务 . 您的测试套件将更易于维护,并且不依赖于外部服务 . 你也可以使用H2's compatibility mode with PostgreSQL .

相关问题