0
@Test
void testDepartment() throws Exception {

    String nameParam1 = "ComputerScience";
    String nameParam2 = "Department of Mathematics";
    String nameParam3 = "Department of Music";
    String nameParam4 = "Department of Language and Literature";
    String locParam = "null";

    List<Department> result = seacrhDepartment(nameParam1, nameParam2, nameParam3, nameParam4, locParam);
    assertThat(result.size()).isEqualTo(0);

}

private List<Department> seacrhDepartment(String nameParam1, String nameParam2, String nameParam3, String nameParam4, String locParam) {

    BooleanBuilder builder = new BooleanBuilder();

    if (nameParam1 != null) {
        builder.and(department.name.eq(nameParam1));
    }

    if (nameParam2 != null) {
        builder.and(department.name.eq(nameParam2));
    }

    if (nameParam3 != null) {
        builder.and(department.name.eq(nameParam3));
    }

    if (nameParam4 != null) {
        builder.and(department.name.eq(nameParam4));
    }

    if (locParam != null) {
        builder.and(department.name.eq(locParam));
    }

    return queryFactory
            .selectFrom(department)
            .where(builder)
            .fetch();
}





@BeforeEach
public void before() {
    queryFactory = new JPAQueryFactory(em);
    Department department1 = new Department("ComputerScience");
    Department department2 = new Department("Department of Mathematics");
    Department department3 = new Department("Department of Music");
    Department department4 = new Department("Department of Language and Literature");
    em.persist(department1);
    em.persist(department2);
    em.persist(department3);
    em.persist(department4);

}

I pre-entered the data.. But Why does isEqual(0) have to be passed here..? Since all conditions were set to builder.and, wouldn't it pass when set to isEqualTo(5)?

And what test should I use to search for the word com and get a computerscience department?

Jaeyeon
  • 25
  • 5

1 Answers1

0

There is no Department that satisfied all filters. You probably want to use builder.or instead of builder.and.

  • Thanks for your comment! but, when i switched to `builder.or` org.opentest4j.AssertionFailedError: expected: 5 but was : 4 Expected :5 Actual :4 This error message occurs. So, if I change it to isEqualTo(4) , it passes. look at the query /* select department from Department department where department.name = ?1 or department.name = ?2 or department.name = ?3 or department.name = ?4 or department.name = ?5 */ The query for locParam is not outputting. Do you know why? – Jaeyeon Jul 21 '21 at 02:57
  • You need to use isNull() instead of eq(“null”). First, nulls in SQL cannot be compared using =: null = null always returns false. The proper comparison is “null is null”. Second, your expression compared against is not a null expression but rather a string expression containing the text “null”. For an actual nullexpression you need Expressions.nullExpression() in Querydsl. – Jan-Willem Gmelig Meyling Jul 21 '21 at 06:34
  • Even if I change to "String", not "Null", isEqualTo(4) is the same. – Jaeyeon Jul 21 '21 at 14:15
  • There are four departments, obviously there are four departments in the query results… – Jan-Willem Gmelig Meyling Jul 21 '21 at 17:05
  • Oops.. Thank you. I have one more question. I want to do something similar to SQL's LIKE %com% when I search asertThat for %com%. `containsOnly` or `containsExactlyInAnyOrder`I think it's use this. – Jaeyeon Jul 22 '21 at 05:08