2016-11-25 19 views
1

我用Spring Boot,PostgreSQL,Maven和JUnit製作了一個簡單的Web應用程序。而在我的IDE中運行它(MVN明確的驗證),一切都運行完美,但在特拉維斯CI運行時,我得到這個異常:在Travis CI,Spring Boot和PostgreSQL中運行EntityManagerException

錯誤創建名稱爲豆「的entityManagerFactory」在類路徑資源定義[組織/ springframework的/ boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]:調用init方法失敗;嵌套異常是javax.persistence.PersistenceException:[PersistenceUnit:default]無法建立休眠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數據庫的連接,它的工作原理(你可以看到它的圖片),但我沒有實體管理器的測試。 有人可以告訴我這是什麼嗎?

回答

1

我會建議使用H2來進行集成測試,而不是PostgreSQL。您不需要設置任何TravisCI服務。您的測試套件將更易於維護,而不依賴於外部服務。您也可以使用H2's compatibility mode with PostgreSQL

+0

感謝您的回答,但我想知道,如何使用PostgreSQL避免此異常。 –

+0

我解決了我的問題。我將我的項目分成兩個配置文件(測試和發佈),併爲每個配置文件創建自己的數據庫。這樣,我就擁有'release'配置文件的PostgreSQL數據庫以及'test'配置文件的H2數據庫,因爲我發現Travis在運行項目時使用嵌入式數據庫。謝謝@luboskmac。 –