xref: /aosp_15_r20/external/mockito/src/test/java/org/mockito/internal/junit/util/JUnitFailureHackerTest.java (revision ceffa222020788e9b81e120db61d5f29c922f983)
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.junit.util;
6 
7 import org.assertj.core.api.Assertions;
8 import org.junit.Test;
9 import org.junit.runner.Description;
10 import org.junit.runner.notification.Failure;
11 import org.mockito.internal.exceptions.ExceptionIncludingMockitoWarnings;
12 import org.mockitoutil.TestBase;
13 
14 import static org.junit.Assert.assertEquals;
15 import static org.assertj.core.api.Assertions.assertThat;
16 
17 public class JUnitFailureHackerTest extends TestBase {
18 
19     JUnitFailureHacker hacker = new JUnitFailureHacker();
20 
21     @Test
shouldReplaceException()22     public void shouldReplaceException() throws Exception {
23         //given
24         RuntimeException actualExc = new RuntimeException("foo");
25         Failure failure = new Failure(Description.EMPTY, actualExc);
26 
27         //when
28         hacker.appendWarnings(failure, "unused stubbing");
29 
30         //then
31         assertEquals(ExceptionIncludingMockitoWarnings.class, failure.getException().getClass());
32         assertEquals(actualExc, failure.getException().getCause());
33         Assertions.assertThat(actualExc.getStackTrace()).isEqualTo(failure.getException().getStackTrace());
34     }
35 
36     @Test
shouldAppendWarning()37     public void shouldAppendWarning() throws Exception {
38         Failure failure = new Failure(Description.EMPTY, new RuntimeException("foo"));
39 
40         //when
41         hacker.appendWarnings(failure, "unused stubbing blah");
42 
43         //then
44         assertThat(failure.getException()).hasMessageContaining("unused stubbing blah");
45     }
46 
47     @Test
shouldNotAppendWhenNoWarnings()48     public void shouldNotAppendWhenNoWarnings() throws Exception {
49         RuntimeException ex = new RuntimeException("foo");
50         Failure failure = new Failure(Description.EMPTY, ex);
51 
52         //when
53         hacker.appendWarnings(failure, "");
54 
55         //then
56         assertEquals(ex, failure.getException());
57     }
58 
59     @Test
shouldNotAppendWhenNullWarnings()60     public void shouldNotAppendWhenNullWarnings() throws Exception {
61         RuntimeException ex = new RuntimeException("foo");
62         Failure failure = new Failure(Description.EMPTY, ex);
63 
64         //when
65         hacker.appendWarnings(failure, null);
66 
67         //then
68         assertEquals(ex, failure.getException());
69     }
70 
71     @Test
shouldPrintTheWarningSoICanSeeIt()72     public void shouldPrintTheWarningSoICanSeeIt() throws Exception {
73         Failure failure = new Failure(Description.EMPTY, new RuntimeException("foo"));
74 
75         //when
76         hacker.appendWarnings(failure, "unused stubbing blah");
77 
78         //then
79         System.out.println(failure.getException());
80     }
81 }
82