i have a doubt about how to pass a JUnit test for a Primefaces FileUploadEvent.
The methods is this:
public void cambiarLogoCrear(FileUploadEvent event) {
cambiarLogo(event, false);
}
private void cambiarLogo(FileUploadEvent event, boolean modificando) {
UploadedFile nuevoLogo = event.getFile();
String nombre = nuevoLogo.getFileName();
if(!nombre.endsWith(".png") && !nombre.endsWith(".jpeg") && !nombre.endsWith(".jpg")) {
FacesMessagesUtils.mostrarMensajeDelProperties("empresa.accion.subirlogo.FICHERO_NO_IMAGEN", COMUN_ERROR_MESSAGE, FacesMessage.SEVERITY_WARN);
}else {
String carpeta = fachada.obtenerRutaParaImagenesDeClase(ClasesRutasEnum.EMPRESA);
if(carpeta != null) {
carpeta = carpeta + "temp" + File.separator;
String nombreArchivo = UUID.randomUUID().toString();
if(modificando) {
FileSystemUtils.borrarFichero(this.empresaEnUso.getLogo());
}
String rutaLogo = FileSystemUtils.intentarSubirImagen(nombreArchivo, nuevoLogo, carpeta);
this.empresaEnUso.setLogo(rutaLogo);
}else {
FacesMessagesUtils.mostrarMensajeDelProperties("empresa.accion.subirlogo.ERROR_CARPETA_IMAGENES_EMPRESAS", COMUN_ERROR_MESSAGE,
FacesMessage.SEVERITY_ERROR);
logger.error("Error al obtener la carpeta de imagenes temporales para empresas");
}
}
}
In the test case i try to use a fake FileUploadEvent with this code
UploadedFile uploadedFileNormal = new UploadedFile() {
@Override
public void write(String filePath) throws Exception {
// Do nothing
}
@Override
public long getSize() {
return 0;
}
@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(file);
}
@Override
public String getFileName() {
return "defaultUser.png";
}
@Override
public String getContentType() {
return "application/octet-stream";
}
@Override
public byte[] getContent() {
try {
return Files.readAllBytes(file.toPath());
}catch (Exception e) {
return null;
}
}
@Override
public void delete() throws IOException {
// Do nothing
}
};
FileUploadEvent event = new FileUploadEvent(null, uploadedFileNormal);
but this return a java.lang.NoClassDefFoundError: javax/el/ELException
then i try to create a FileUpload component to pass de FileUploadEvent:
FileUpload input = new FileUpload();
FileUploadEvent event = new FileUploadEvent(input, uploadedFileNormal);
but returns the same error: java.lang.NoClassDefFoundError: javax/el/ELException
How can I realize what I want?
Versions of Software:
- Java 8
- Primefaces 11
- JUnit 5.9
Many Thanks