• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.base;
16 
17 import static com.google.common.base.Verify.verify;
18 import static com.google.common.base.Verify.verifyNotNull;
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.annotations.J2ktIncompatible;
24 import junit.framework.AssertionFailedError;
25 import junit.framework.TestCase;
26 
27 /** Unit test for {@link com.google.common.base.Verify}. */
28 @GwtCompatible(emulated = true)
29 public class VerifyTest extends TestCase {
testVerify_simple_success()30   public void testVerify_simple_success() {
31     verify(true);
32   }
33 
testVerify_simple_failure()34   public void testVerify_simple_failure() {
35     try {
36       verify(false);
37       fail();
38     } catch (VerifyException expected) {
39     }
40   }
41 
testVerify_simpleMessage_success()42   public void testVerify_simpleMessage_success() {
43     verify(true, "message");
44   }
45 
testVerify_simpleMessage_failure()46   public void testVerify_simpleMessage_failure() {
47     try {
48       verify(false, "message");
49       fail();
50     } catch (VerifyException expected) {
51       assertThat(expected).hasMessageThat().isEqualTo("message");
52     }
53   }
54 
testVerify_complexMessage_success()55   public void testVerify_complexMessage_success() {
56     verify(true, "%s", IGNORE_ME);
57   }
58 
testVerify_complexMessage_failure()59   public void testVerify_complexMessage_failure() {
60     try {
61       verify(false, FORMAT, 5);
62       fail();
63     } catch (VerifyException expected) {
64       checkMessage(expected);
65     }
66   }
67 
68   private static final String NON_NULL_STRING = "foo";
69 
testVerifyNotNull_simple_success()70   public void testVerifyNotNull_simple_success() {
71     String result = verifyNotNull(NON_NULL_STRING);
72     assertSame(NON_NULL_STRING, result);
73   }
74 
testVerifyNotNull_simple_failure()75   public void testVerifyNotNull_simple_failure() {
76     try {
77       verifyNotNull(null);
78       fail();
79     } catch (VerifyException expected) {
80     }
81   }
82 
testVerifyNotNull_complexMessage_success()83   public void testVerifyNotNull_complexMessage_success() {
84     String result = verifyNotNull(NON_NULL_STRING, "%s", IGNORE_ME);
85     assertSame(NON_NULL_STRING, result);
86   }
87 
testVerifyNotNull_simpleMessage_failure()88   public void testVerifyNotNull_simpleMessage_failure() {
89     try {
90       verifyNotNull(null, FORMAT, 5);
91       fail();
92     } catch (VerifyException expected) {
93       checkMessage(expected);
94     }
95   }
96 
97   @J2ktIncompatible
98   @GwtIncompatible // NullPointerTester
testNullPointers()99   public void testNullPointers() {
100     // Don't bother testing: Verify is like Preconditions. See the discussion on that class.
101   }
102 
103   private static final Object IGNORE_ME =
104       new Object() {
105         @Override
106         public String toString() {
107           throw new AssertionFailedError();
108         }
109       };
110 
111   private static final String FORMAT = "I ate %s pies.";
112 
checkMessage(Exception e)113   private static void checkMessage(Exception e) {
114     assertThat(e).hasMessageThat().isEqualTo("I ate 5 pies.");
115   }
116 }
117