java.lang.AssertionError:expected:<on>但是:<com.spacestudy.model.account@783f5f71>

时间:2018-05-09 14:41:56

标签: unit-testing spring-data-jpa junit4 querydsl

我正在尝试为以下搜索过滤器方法编写JUnit测试用例。我使用Querydsl作为搜索过滤器。以下方法不依赖于Repository;这就是为什么我不使用mockito。这是我第一次为这种方法编写测试用例。如果您有任何建议,请提出任何建议吗?

public List<Tuple> btnSearchClick(String sClientAcctId, String sAcctDesc, String sInvestigatorName,
            String sClientDeptId) throws Exception {

        QAccount account = QAccount.account;
        QDepartment department = QDepartment.department;
        QAccountCPCMapping accountCPCMapping = QAccountCPCMapping.accountCPCMapping;
        QInvestigator investigator = QInvestigator.investigator;

        JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
        query.select(Projections.bean(Account.class, account.sClientAcctId, account.sAcctDesc, account.sLocation,
                Projections.bean(Department.class, department.sDeptName, department.sClientDeptId).as("department"),
                Projections.bean(Investigator.class, investigator.sInvestigatorName).as("investigator"),
                Projections.bean(AccountCPCMapping.class, accountCPCMapping.sCCPCode).as("accountCPC"))).from(account)
                .innerJoin(account.department, department).innerJoin(account.accountCPC, accountCPCMapping)
                .innerJoin(account.investigator, investigator).where(account.nInstId.eq(60));

测试用例

@Test
public void btnSearchClick() throws Exception {

List<Tuple> account= accountService.btnSearchClick("1124100", sAcctDesc, sInvestigatorName, sClientDeptId);


Department dep = new Department();
dep.setsDeptName("Deans Office");
dep.setsClientDeptId("120010");

Investigator invest = new Investigator();
invest.setsInvestigatorName("Ram, Sri");

AccountCPCMapping cpc = new  AccountCPCMapping();
cpc.setsCCPCode("RT");


Account acc = new Account();
acc.setsLocation("ON");
acc.setsAcctDesc("SRIRAM");
acc.setsClientAcctId("1124100");

acc.setInvestigator(invest);
acc.setDepartment(dep);
acc.setAccountCPC(cpc); 

accountRepo.save(acc);

assertEquals(acc.getsLocation(), account.get(0));
}

Account.java

@Entity
@Table(name = "account")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Account implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "account_seq_generator")
    @SequenceGenerator(name = "account_seq_generator", sequenceName = "account_seq")

    @Column(name = "naccount_id")
    public Integer nAccountId;

    @Column(name = "namount")
    public String nAmount;

    @Column(name = "sacct_desc")
    public String sAcctDesc;

    @Column(name = "naccount_cpc_mapping_id")
    public Integer nAccountCPCMappingId;

    @Column(name = "nindirect_cost_rate")
    public Integer nIndiretCostRate;

    @Column(name = "nagency_id")
    public Integer nAgencyId;

    @Column(name = "ndept_id")
    public Integer nDeptId;

    @Column(name = "sgrant_num")
    public String sGrantNum;

    @Column(name = "dstart_date")
    public Timestamp dStartDate;

    @Column(name = "dend_date")
    public Timestamp dEndDate;

    @Column(name = "slocation")
    public String sLocation;

    @Column(name = "sclient_acct_id")
    public String sClientAcctId;

    @Column(name = "ninvestigator_id")
    public Integer nInvestigatorId;

    @Column(name = "ninst_id")
    public Integer nInstId;

    @Column(name = "ntemp_account_id")
    public Integer nTempAccountId;

    @ManyToOne(optional = true, cascade = { CascadeType.MERGE })
    @JoinColumn(name = "ndept_id", insertable = false, updatable = false)
    public Department department;

    @ManyToOne(optional = true, cascade = { CascadeType.ALL })
    @JoinColumn(name = "ninvestigator_id", insertable = false, updatable = false)
    public Investigator investigator;

    @ManyToOne(optional = true, cascade = { CascadeType.ALL })
    @JoinColumn(name = "naccount_cpc_mapping_id", insertable = false, updatable = false)
    public AccountCPCMapping accountCPC;

    // Getter and Setter

堆栈跟踪

java.lang.AssertionError: expected:<ON> but was:<com.spacestudy.model.Account@783f5f71>
at org.junit.Assert.fail(Assert.java:88)
at org.junit.Assert.failNotEquals(Assert.java:834)
at org.junit.Assert.assertEquals(Assert.java:118)
at org.junit.Assert.assertEquals(Assert.java:144)
at com.spacestudy.service.TestAccountService.btnSearchClick(TestAccountService.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

邮递员的输出

[
    {
        "sAcctDesc": "SRIRAM",
        "sLocation": "ON",
        "sClientAcctId": "1124100",
        "department": {
            "sDeptName": "Deans Office",
            "sClientDeptId": "120010"
        },
        "investigator": {
            "sInvestigatorName": "Ram, Sri"
        },
        "accountCPC": {
            "sCCPCode": "RT"
        }
    }
]

任何人都可以告诉我在我的测试用例中我做错了什么,或建议替代上面的代码。

1 个答案:

答案 0 :(得分:1)

列表为零索引。这意味着如果您的列表只有1个元素,那么它的索引将是0,而不是1。 索引为1的元素实际上是第二个元素(它不存在,因此索引超出范围)。

account.get(1)

应该是

account.get(0);