Junit with Mockito for java

94 Views Asked by At

I am learning java and exploring junit with Mockito. The idea is to understand where and how to use mockito and learn how to write junit tests. Im a beginner and learning.

How can i write a junit test with mockito for the following code :

public class QueryBuilder<EntityT> {
private List<List<String>> allConditions;
private int top;
private int skip;
private FieldOrdering ordering;
private String tableName;

public QueryBuilder() {
    this.allConditions = new ArrayList<>();
    this.top = -1;
    this.skip = 0;
    this.ordering = new FieldOrdering();
    this.tableName = "your_table"; // Default table name
}

public QueryBuilder<EntityT> filter(FilterableExpression filters) {
    allConditions.add(filters.getConditions());
    return this;
}

public QueryBuilder<EntityT> and() {
    allConditions.get(allConditions.size() - 1).add(" AND ");
    return this;
}

public QueryBuilder<EntityT> or() {
    allConditions.get(allConditions.size() - 1).add(" OR ");
    return this;
}

public QueryBuilder<EntityT> top(int count) {
    this.top = count;
    return this;
}

public QueryBuilder<EntityT> skip(int count) {
    this.skip = count;
    return this;
}

public QueryBuilder<EntityT> orderBy(FieldOrdering ordering) {
    this.ordering = ordering;
    return this;
}

public QueryBuilder<EntityT> tableName(String tableName) {
    this.tableName = tableName;
    return this;
}

/**
 * build the query with the clauses addded to Filter Expression *
 * 
 * @return
 */
public String build() {
    StringBuilder query = new StringBuilder("SELECT * FROM " + tableName + " WHERE ");

    if (!allConditions.isEmpty()) {
        for (int i = 0; i < allConditions.size(); i++) {
            List<String> conditions = allConditions.get(i);
            for (int j = 0; j < conditions.size(); j++) {
                query.append(conditions.get(j));
                if (j < conditions.size() - 1 && !conditions.get(j + 1).equals(" AND ")
                        && !conditions.get(j + 1).equals(" OR ")) {
                    query.append(" ");
                }
            }
            if (i < allConditions.size() - 1) {
                query.append(" AND ");
            }
        }
    } else {
        query.append("1=1");
    }

    List<String> orderByList = ordering.getOrderByList();
    if (!orderByList.isEmpty()) {
        query.append(" ORDER BY ").append(String.join(", ", orderByList));
    }

    if (top > 0) {
        query.append(" LIMIT ").append(top);
    }

    if (skip > 0) {
        query.append(" OFFSET ").append(skip);
    }

    return query.toString();
}

}

Any help/suggestion is highly appreciated. I want to write the test, check the flow and understand how everything works.

Thanks in advance.

1

There are 1 best solutions below

3
DimXenon On

UPD: It's just an illustration code here (use it like pattern for testing your logic):

@Mock
QueryBuilder queryBuilder;

@Test
void mockBuildMethodTest(){

  String predefined = "SOME PREDEFINED SQL";

  Mockito.doReturn(predefined)
    .when(queryBuilder)
    .build();

  Assertions.assertEquals(predefined, queryBuilder.build());

}