【问题标题】:How to write JUnit 5 Test for mapstruct mapper via Eclipse and SpringBoot如何通过 Eclipse 和 Spring Boot 为 mapstruct 映射器编写 JUnit 5 测试
【发布时间】:2021-01-04 20:22:21
【问题描述】:

基本上我只是想测试我的映射器,它应该由 MapStruct 自动生成。

我尝试了所有可能的注释,this answer 似乎是最好的解决方案,以便与 sprinboot 和 junit 5 一起工作,尽管它在我的 Eclipse 中仍然无法工作,但它只是显示一个错误,如下所示 ProductMapperImpl cannot be resolved to a type虽然它应该是通过 @Mapper 注释自动生成的。 例如,我也在使用 Lombok Annotation,所有这些自动生成的方法 eclipse 都可以识别并且工作得很好。

那么我做错了什么?也许还有另一种方法可以将映射器接口自动连接到测试类以便它工作?也许它与生成的源文件夹有关,只是 eclipse 不知何故无法识别它?

TestClass

@Slf4j
@SpringBootTest(classes = { ProductMapperImpl.class })
class ProductMapperTest {

  @Autowired
  private ProductMapper productMapper;

  @BeforeAll
  public static void setup() {
    log.info("Testing mapper between product entity and product dto...");
  }

  @Test
  public void test() {
    String hello = "Hello";
    assertTrue("Hello".equals(hello));
  }

  @Test
  public void shouldMapProductToDto() {
    // given
    User user1 = new User("username_1", "1", Role.STORE);
    RetailStore store1 = new RetailStore(user1, "store", "1234", "store@example.org");
    Category cat1 = new Category("vegetables");
    Product entity = new Product(cat1, "cucumber", new BigDecimal(0.99), store1);
    // when
    System.out.println(entity);
    ProductDto dto = productMapper.productToProductDto(entity);
    // then
    assertNotNull(dto);
    assertEquals(dto.getCategoryName(), entity.getCategory().getCatName());
    assertEquals(dto.getName(), entity.getName());
    assertEquals(dto.getPrice(), entity.getPrice());
    assertEquals(dto.getProductId(), entity.getProductId());
    assertEquals(dto.getStoreId(), entity.getRetailStore().getStoreId());
  }

  @Test
  public void shouldMapDtoToProduct() {
    // given
    ProductDto dto = new ProductDto();
    dto.setCategoryName("vegetables");
    dto.setDescription("description");
    dto.setLimitations("limitations");
    dto.setName("cucumber");
    dto.setPrice(new BigDecimal(1.99));
    dto.setProductId(1L);
    dto.setRemainingStock(1);
    dto.setStoreId(1L);
    // when
    Product entity = productMapper.productDtoToProduct(dto);
    // then
    assertNotNull(entity);
    assertEquals(entity.getCategory().getCatName(), dto.getCategoryName());
    assertEquals(entity.getDescription(), dto.getDescription());
    assertEquals(entity.getLimitations(), dto.getLimitations());
    assertEquals(entity.getName(), dto.getName());
    assertEquals(entity.getPrice(), dto.getPrice());
    assertEquals(entity.getProductId(), dto.getProductId());
    assertEquals(entity.getRemainingStock(), dto.getRemainingStock());
    assertEquals(entity.getRetailStore().getStoreId(), dto.getStoreId());
    assertEquals(entity.getRetailStore().getStoreId(), dto.getStoreId());
  }

}

MapperClass

@Mapper(componentModel = "spring")
public interface ProductMapper {

  /**
   * Map product entity instance to product Dto instance.
   *
   * @param product entity product
   * @return dto product
   */
  @Mappings({ @Mapping(source = "category.catName", target = "categoryName"),
      @Mapping(source = "retailStore.storeId", target = "storeId") })
  ProductDto productToProductDto(Product product);

  /**
   * Map product Dto instance to product entity instance.
   *
   * @param productDto dto product
   * @return entity product
   */
  @Mappings({ @Mapping(source = "categoryName", target = "category.catName"),
      @Mapping(source = "storeId", target = "retailStore.storeId") })
  Product productDtoToProduct(ProductDto productDto);

  /**
   * Update product with the latest values from a product DTO.
   *
   * @param productDto dto product
   * @param product    entity product
   */
  @Mappings({ @Mapping(source = "categoryName", target = "category.catName"),
      @Mapping(source = "storeId", target = "retailStore.storeId") })
  void updateModel(ProductDto productDto, @MappingTarget Product product);

  // aggregated root

  /**
   * Map list of product entities to list of product DTOs.
   *
   * @param products List of product entities
   * @return list of product dto`s
   */
  @Mappings({ @Mapping(source = "category.catName", target = "categoryName"),
      @Mapping(source = "retailStore.storeId", target = "storeId") })
  List<ProductDto> toProductDtos(List<Product> products);

}

产品 dto

@Data
public class ProductDto {
  private Long productId;
  private String name;
  private String categoryName;
  private Long storeId;
  private BigDecimal price;
  private byte[] picture;
  private String description;
  private String limitations;
  private Integer remainingStock;
}

产品实体

@Data
@Entity
public class Product {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(name = "product_id")
  private Long productId;

  // defines foreign key column category_id and indicates the owner of the
  // ManyToOne relationship
  @ManyToOne
  @JoinColumn(name = "category_id")
  private Category category;

  @Lob
  @Basic
  private byte[] picture;

  private String name;

  private BigDecimal price;

  // defines foreign key column store_id and indicates the owner of the ManyToOne
  // relationship
  @JsonBackReference(value = "product-store")
  @ManyToOne
  @JoinColumn(name = "store_id")
  private RetailStore retailStore;

  private String description;

  private String limitations;

  @Column(name = "remaining_stock")
  private Integer remainingStock;

  /**
   * Constructor.
   */
  protected Product() {
  }

  /**
   * Constructor.
   *
   * @param category    category of the product
   * @param name        name of the product
   * @param price       price of the product
   * @param retailStore retail store selling this product
   */
  public Product(Category category, String name, BigDecimal price, RetailStore retailStore) {
    super();
    this.category = category;
    this.name = name;
    this.price = price;
    this.retailStore = retailStore;
  }

  /**
   * Converting bigDecimal scale (number of digits to the right of the decimal
   * point) of price to 2.
   */
  @PrePersist
  @PreUpdate
  public void pricePrecisionConvertion() {
    this.price.setScale(2, RoundingMode.HALF_UP);
  }

【问题讨论】:

  • 我也有同样的问题

标签: java eclipse spring-boot mapstruct


【解决方案1】:

@SpringBootTest(classes = { ProductMapperImpl.class })

 @Autowired
      private ProductMapperImpl productMapperImpl ;  *<- add this*

【讨论】:

  • 试过了,但我仍然得到同样的错误,现在在新添加的 Autowired ProductMapperImpl 实例中。
猜你喜欢
  • 2017-12-29
  • 1970-01-01
  • 2023-01-15
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 2021-07-26
  • 1970-01-01
相关资源
最近更新 更多