Checking 2 expected values ​​in Junit


I have a java program that throws an exception with two different messages for two different scenarios, and I want the Junit test case to check equality for both of these messages. As an example -

public void amethod() {
           // do some processing
        if(scenario1 == true) {
            throw new MySystemException("An error occured due to case 1 being incorrect.");
        }
        else if(scenario2 == true) {
            throw new MySystemException("An error occured as case 2 could not be found");
        }
    }  

Now JUnit for this will be something like <

public void testAMethod() {
    // do something
    assertEquals("Expected", "Actual");
}

, , Scenario1, junit , Scenario2 .
, - , Junit, test method , ?
- OR, , .
, .

UPDATE

, - .
, , , . , , , . , ,

public void testAMethodScenario1() {
    // do the necessary
    assertEquals("Expected Exception Message 1", "Actual");
}

public void testAMethodScenario2() {
    // do the necessary
    assertEquals("Expected Exception Message 2", "Actual");
}  

.

+3
7

, : , .

, JUnit4:

@Test(expected=MySystemException.class)
public void testException() {
   amethod();
}

try-catch :

@Test
public void testException() {
   try {
      amethod();
      fail("MySystemException expected");
   }
   catch (MySystemException e) {
      // Success!
   }
}

, try-catch , AssertEquals catch.

. .

, , , Hamcrest.

, - (untested - ):

@Test
public void testException() {
   try {
      amethod();
      fail("MySystemException expected");
   }
   catch (MySystemException e) {
      String expectedMessage1 = "An error occured due to case 1 being incorrect.";
      String expectedMessage2 = "An error occured as case 2 could not be found";
      assertThat(e.getMessage(), 
                 anyOf(equalTo(expectedMessage1), equalTo(expectedMessage2)));
   }
}
+2

, ( ) :

try {
    // trigger scenario 1
    fail("An exception should have been thrown here !");
} catch (MySystemException e1) {
    assertEquals("Wrong error message", m1, e1.getMessage());
}

try {
    // trigger scenario 2
    fail("An exception should have been thrown here !");
} catch (MySystemException e2) {
    assertEquals("Wrong error message", m2, e2.getMessage());
}

, , , , " /" .:)

+5

, ? , . , - - , :

@Test
public void testAmethodThrowsException() {
    try {
        amethod();
        fail("amethod() should have thrown an exception");
    }
    catch (MySystemException e) {
        String msg = e.getMessage();
        assertTrue("bad message: " + msg, msg.equals("An error occured due to case 1 being incorrect.") || msg.equals("An error occured as case 2 could not be found"));
    }
}
+1

, , API. , .

, - :

/**
 * Do something.
 * @throws MySystemException1 in case 1.
 * @throws MySystemException2 if Foo not found.
 */
public void amethod() {
   // do some processing 
   if(scenario1 == true) {
      throw new MySystemException1("Case 1.");
    }
    else if(scenario2 == true) {
        throw new MySystemException2("Foo not found");
    }
}  
+1

JUnit 4 ( Exception.class)

@Test(expected= MySystemException.class) public void empty() { 
    // what ever you want
}

Google: Expected JUnit exceptions for more information.

0
source

@ Relay in JUnit4:

    public class ExceptionRule implements MethodRule {
        @Override
        public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {
                    try {
                        base.evaluate();
                        Assert.fail();
                    } catch (MySystemException e) {
                         if(scenario1)
                            assertEquals("Expected error message1", e1.getMessage();
                         if(scenario2)
                            assertEquals("Expected error message2",e1.getMessage();
                }
            }
        };    
    }
}

In the test folder, use the rule:

 @Rule public ExceptionRule rule = new ExceptionRule();
0
source

BDD Style Solution with Catch Elimination

@Test
public void testAMethodScenario1() {

    //given scenario 1

    when(foo).amethod();

    then(caughtException())
            .isInstanceOf(MySystemException.class)
            .hasMessage("An error occured due to case 1 being incorrect.");
}

@Test
public void testAMethodScenario2() {

    //given scenario 2

    when(foo).amethod();

    then(caughtException())
            .isInstanceOf(MySystemException.class)
            .hasMessage("An error occured as case 2 could not be found");
}

Source

Dependencies

com.googlecode.catch-exception:catch-exception:1.2.0
0
source

Source: https://habr.com/ru/post/1784928/


All Articles