I made a simple web application using Spring Boot, PostgreSQL, Maven and JUnit. While running it in my IDE (mvn clear-verify) everything works perfect but while running in Travis CI I got this exception:
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
and a lot of others. My tests run perfect as well in IDE. Can someone tell me why? My code is here:
Entity:
@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:
@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();
}
}
Controller:
@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
And 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
I checked the connection to PostgreSQL DB and it works (you can see it on picture), but I do not have Entity Manager in Test. Can somebody tell my what is it?
I would suggest to use H2 for integration testing instead of PostgreSQL. You don't need to setup any TravisCI service. You test suite will be much more maintainable and not dependent on external service. You can also use H2's compatibility mode with PostgreSQL.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments