Tengo una duda al pasar un test en Java y un throw ProductException el cual no me deja avanzar con la tarea. Adjunto una foto del diagrama por si es necesario facilitar mas codigo.
public abstract class PhysicalProduct extends Product implements Stockable {
private int stock;
Campaign campaign;
ProductException productException;
Book book;
// Constructor por defecto clase Product
public PhysicalProduct() throws ProductException {
super();
}
// Constructor parametrizado con atributos heredados.
public PhysicalProduct(String name, int publicationYear, String description, double price, int stock) throws ProductException {
setName(name);
setPublicationYear(publicationYear);
setDescription(description);
setPrice(price);
setStock(stock);
}
// Método que da error:
public void activePromotion(Promotion promotion) throws ProductException {
if (campaign == Campaign.BOOK_DAY || campaign == Campaign.CHRISTMAS) {
setPromotion(promotion);
} else if( campaign == Campaign.SUMMER || campaign == Campaign.BLACK_FRIDAY) {
throw new ProductException(MSG_ERR_PROMOTION_NOT_ALLOWED);
}
}
Este es el test que no consigo pasar por el siguiente error:
Exception to be thrown, but nothing was thrown.
@Test
@Order(3)
void testActivePromotion() {
assertNull(book.getPromotion());
Promotion promotion1 = new Promotion(20, Campaign.SUMMER, 7);
Promotion promotion2 = new Promotion(15, Campaign.CHRISTMAS, 5);
try{
Exception ex = assertThrows(Exception.class, () -> book.activePromotion(promotion1));
assertEquals(ProductException.MSG_ERR_PROMOTION_NOT_ALLOWED, ex.getMessage());
book.activePromotion(promotion2);
assertEquals(promotion2, book.getPromotion());
book.setPromotion(null);
assertNull(book.getPromotion());
}catch(ProductException e) {
fail("testActivePromotion failed");
}
}
Os adjunto también la clase ProductException donde no debería existir ningún problema:
public class ProductException extends Exception{
public static String MSG_ERR_NAME = "[ERROR] Product's name cannot be longer than 50 characters";
public static String MSG_ERR_PUBLICATION_YEAR = "[ERROR] Product's publicationYear cannot be later than current year";
public static String MSG_ERR_DESCRIPTION = "[ERROR] Product's description cannot be longer than 250 characters";
public static String MSG_ERR_NEGATIVE = "[ERROR] Product's price cannot be negative";
public static String MSG_ERR_PARAMETER_INCORRECT = "[ERROR] parameter is not the correct format";
public static String MSG_ERR_PROMOTION_NOT_ALLOWED = "[ERROR] Product's promotion are not allowed";
public ProductException() {
super();
}
public ProductException(String message) {
super(message);
}
}
La clase Book ya que el test es a partir de un objeto Book:
public class Book extends PhysicalProduct{
private int pages = 1;
private String isbn = "9780000000000";
ProductException productException;
public Promotion promotion;
public Book() throws ProductException{
}
public Book(String name, int publicationYear, String description, double price, int stock, int pages) throws ProductException {
setName(name);
setPublicationYear(publicationYear);
setDescription(description);
setPrice(price);
setStock(stock);
setPages(pages);
}
public Book(String name, int publicationYear, String description, double price, int stock, int pages, String isbn) throws Exception {
setName(name);
setPublicationYear(publicationYear);
setDescription(description);
setPrice(price);
setStock(stock);
setPages(pages);
setIsbn(isbn);
}
Y la superclase Product que es donde se encuentra el método principal getPromotion y setPromotion:
public abstract class Product {
private static int referenceId = 15000;
private int reference;
private String name;
private int publicationYear;
private String description;
private double price;
private VAT percentage = VAT.STANDARD;
ProductException productException;
public Promotion promotion;
private int sales = 0;
public Product() throws ProductException {
this("Lorem Ipsum", 2011, "lorem ipsum description", 7.50 );
}
public Product(String name, int publicationYear, String description, double price) throws ProductException {
setName(name);
setPublicationYear(publicationYear);
setDescription(description);
setPrice(price);
createReference();
}
public Promotion getPromotion() {return promotion;}
protected Promotion setPromotion(Promotion promotion) {
this.promotion = promotion;
return promotion;
}
Muchas gracias
campaign == Campaign.SUMMER || campaign == Campaign.BLACK_FRIDAY
, pero esecampaign
hace referencia alcampaign
de la clase, no al de lapromotion
que le pasas al método!! Deberías hacer las comparaciones conpromotion.getCampaign()
promotion
y un objeto de tipocampaign
cuando en teoríacampaign
pertenece apromotion
y no aphysicalproduct