Search code examples
selenium-webdrivertestngassertions

How to assert an actual value against 2 or more expected values in TestNG


Automating an insurance application. Writig TestNG assertions for selenium web driver tests to verify the start date of the policy, which is by default today's date.

But in some cases it can be tomorrow's date depending on insurers and schemes. I want to include this in the assertion. But I want to validate for any of these two, start date=Today or Tomorrow. want to combine following 2 assertions.

I can do it by creating a third boolean variable and check the date to see if it matches with one of these and making the boolean true and do a boolean assertion, anyone know any other way to do this directly in testNG.

1.    Assert.assertEquals(start_date, date_today, "Assertion failed-  Start Date");

2.    Assert.assertEquals(start_date, date_tomorrow, "Assertion failed-  Start Date");

In junit we have something like

assertThat(result, isOneOf("value1", "value2"));

is available to assert similar situation.

Can we do same operation in testNG?


Solution

  • Just as with this similar question, not implicitly. BTW assertThat in JUnit relies probably on a Hamcrest matcher, maybe an older version. Anyway you have at least the following options:

    • use TestNG's assertTrue
    • use an additional library such as Hamcrest, AssertJ, etc

    Dependencies:

        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.9.0</version>
            <scope>test</scope>
        </dependency>
    

    Code:

    import org.testng.annotations.Test;
    
    import java.time.LocalDate;
    
    import static org.assertj.core.api.Assertions.assertThat;
    import static org.hamcrest.CoreMatchers.anyOf;
    import static org.hamcrest.CoreMatchers.equalTo;
    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.testng.Assert.assertTrue;
    
    public class SomeTest {
        @Test // compare strings (usually that's what you get as values from a webpage)
        public void shouldCheckMultipleStringValues() {
            // simulate some data
            String myText = "my text";
    
            // TestNG
            assertTrue(myText.equals("my text") || myText.equals("your text") || myText.equals("his text"));
    
            //Hamcrest
            assertThat(myText, anyOf(equalTo("my text"), equalTo("your text"), equalTo("his text")));
    
            // AssertJ
            assertThat(myText).isIn("my text", "your text", "his text");
        }
    
        @Test // but you can even compare dates if you need to
        public void shouldCheckMultipleDateValues() {
            // simulate some date
            LocalDate myDate = LocalDate.now();
    
            // simulate a bunch of expected dates
            LocalDate yesterday = LocalDate.now().minusDays(1);
            LocalDate now = LocalDate.now();
            LocalDate tomorrow = LocalDate.now().plusDays(1);
    
            // TestNG
            assertTrue(myDate.equals(yesterday) || myDate.equals(now) || myDate.equals(tomorrow));
    
            //Hamcrest
            assertThat(myDate, anyOf(equalTo(yesterday), equalTo(now), equalTo(tomorrow)));
    
            // AssertJ
            assertThat(myDate).isIn(yesterday, now, tomorrow);
            assertThat(myDate).isBetween(yesterday, tomorrow);
        }
    }