Writing a Java Arquillian test for a Map Tuple method

58 Views Asked by At

I'm attempting to write an Arquillian test to test the following method:

    // provinceName is key, provinceAbbreviation is value
    public Map<String, String> regionMap() {
        return em.createQuery("""
            select distinct p.provinceName as prov_id, p.provinceAbbreviation as prov_value
            from CanadianPersonalIncomeTaxRate p
            order by p.provinceName """, Tuple.class)
                .getResultStream()
                .collect(Collectors.toMap(
                                tuple -> tuple.get(0, String.class),
                                tuple -> tuple.get(1, String.class)
                        )
                );
    }

The purpose of the method is to map through my list of canadian provinces with tax information, and return one value for each unique canadian province. The expected output is the @CsvSource below Here is my test method currently:

  @Order(2)
    @ParameterizedTest
    @CsvSource({
            "13,Alberta,AB,Yukon,YT"
    })
    void regionMap(int expectedSize, String firstProvinceName, String firstProvinceAbbreviation,
                   String lastProvinceName, String lastProvinceAbbreviation) {
        assertNotNull(_canadianPersonalIncomeTaxRateRepository);
        // Arrange
        Map<String, String> provinceList = _canadianPersonalIncomeTaxRateRepository.regionMap();
        // Assert
        assertEquals(expectedSize, provinceList.size());
        assertEquals(firstProvinceName, provinceList.get(0) );
        assertEquals(firstProvinceAbbreviation, provinceList.get(1) );
       // assertEquals(lastProvinceName, provinceList.get() );
       // assertEquals(lastProvinceAbbreviation, provinceList.get() );

    }

When I attempt to run the test, my error message states that my String values are null: IE Expected is Alberta but return is null. The expected size passes though. Test Error message

I understand that my key is provinceName, and my value is provinceAbbreviation. For my return from the tuple I'm expecting both of these values to be returned. What am I getting wrong for this test to work? Any insight is greatly appreciated

1

There are 1 best solutions below

2
user21313001 On

have you tried adding DISTINCT to your SELECT, i.e. SELECT DISTINCT p.provinceName, ...