编写JPA存储库JUnit,其中一个存储库依赖于Other

时间:2018-02-06 06:39:32

标签: java spring unit-testing junit spring-data-jpa

考虑一个例子,我有两个实体,如下所示

  1. Attribute.java

    @Entity
         @Table(name = "attribute")
         public class Attribute {
            @Id
            @GeneratedValue(strategy=GenerationType.IDENTITY)
            @Getter private Long id;
    
    
        @Column(name="name")
        @Getter private String name;    
    
        @Column(name = "source")
        @Getter private String source;
    
        protected Attribute(){}
        public Attribute(final String name, final String source) {
            this.name = name;
            this.source = source;
        }
    
    }
  2. AttributeGroup.java

        @Column(name="name")
        @Getter private String name;    
    
        @Column(name = "source")
        @Getter private String source;
    
        protected Attribute(){}
        public Attribute(final String name, final String source) {
            this.name = name;
            this.source = source;
        }
    
  3. 两个存储库

    1. AttributeRepository.java

      @Entity @Table(name = "attribute_group") public class AttributeGroup {

          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          @Getter private Long id;
      
          @Column(name="name")
          @Getter private String name;
      
          @Column(name = "value")
          @Getter private String value;
      
          @Getter
          @Column(name="attribute_id", nullable=false)
          protected Long attributeId;
      
          @Getter
          @OneToOne(optional=true, fetch = FetchType.EAGER)
          @JoinColumn(name="attribute_id",  updatable=false, insertable=false,  referencedColumnName="id")
          private Attribute attribute;
      
          protected AttributeGroup(){}
      }
      

    2. AttributeGroupRepository.java

          @Id
          @GeneratedValue(strategy=GenerationType.IDENTITY)
          @Getter private Long id;
      
          @Column(name="name")
          @Getter private String name;
      
          @Column(name = "value")
          @Getter private String value;
      
          @Getter
          @Column(name="attribute_id", nullable=false)
          protected Long attributeId;
      
          @Getter
          @OneToOne(optional=true, fetch = FetchType.EAGER)
          @JoinColumn(name="attribute_id",  updatable=false, insertable=false,  referencedColumnName="id")
          private Attribute attribute;
      
          protected AttributeGroup(){}
      }
      
    3. AttributeGroupRepositoryTest.java

      public interface AttributeRepository{}

      xml文件

      public interface AttributeGroupRepository {
      
      
      /**
       * find list of Groups by attribute name
       * @param attribute
       * @return
       */
      List<AttributeGroup> findByAttribute(Attribute attribute);
      
      }

      以上测试用例抛出异常

      /**
       * find list of Groups by attribute name
       * @param attribute
       * @return
       */
      List<AttributeGroup> findByAttribute(Attribute attribute);
      

      由于此测试依赖于属性存储库,因为Junit是单元测试用例,我不想使用属性存储库。

      是否有其他方法可以模拟其他存储库数据?

2 个答案:

答案 0 :(得分:0)

我找到了不使用其他存储库的方法,我们可以如何执行测试用例。

替换以下行:

List<AttributeGroup> groups = groupRepository.findByAttribute(attribute);

List<AttributeGroup> groups = groupRepository.findByAttributeNameAndAttributeSource("Data","abc");

答案 1 :(得分:-1)

您正在搜索未在存储库中保留的对象,请先从AttributeRepository中找到该属性,然后在属性组存储库中搜索返回的属性!

Attribute attribute=attributeRepository.findByNameAndSource("Data",Source);
if(attribute!=null){
   List<AttributeGroup> groups = groupRepository.findByAttribute(attribute);
}