• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package test.assertion;
2 
3 import org.testng.Assert;
4 import org.testng.annotations.Test;
5 import org.testng.asserts.IAssert;
6 import org.testng.asserts.SoftAssert;
7 
8 import java.util.ArrayList;
9 import java.util.Collection;
10 
11 public class SoftAssertTest {
12 
13   @Test
testOnSucceedAndFailureCalled()14   public void testOnSucceedAndFailureCalled() throws Exception {
15     final Collection<IAssert> succeed = new ArrayList<>();
16     final Collection<IAssert> failures = new ArrayList<>();
17     final SoftAssert sa = new SoftAssert() {
18       @Override
19       public void onAssertSuccess(IAssert assertCommand) {
20         succeed.add(assertCommand);
21       }
22 
23       @Override
24       public void onAssertFailure(IAssert assertCommand, AssertionError ex) {
25         failures.add(assertCommand);
26       }
27     };
28     sa.assertTrue(true);
29     sa.assertTrue(false);
30     Assert.assertEquals(succeed.size(), 1, succeed.toString());
31     Assert.assertEquals(failures.size(), 1, failures.toString());
32   }
33 
34   @Test
testAssertAllCount()35   public void testAssertAllCount() throws Exception {
36     String message = "My message";
37     SoftAssert sa = new SoftAssert();
38     sa.assertTrue(true);
39     sa.assertTrue(false, message);
40     try {
41       sa.assertAll();
42       Assert.fail("Exception expected");
43     } catch (AssertionError e) {
44       String[] lines = e.getMessage().split("\r?\n");
45       Assert.assertEquals(lines.length, 2);
46       lines[1] = lines[1].replaceFirst(message, "");
47       Assert.assertFalse(lines[1].contains(message));
48     }
49   }
50 }
51