• 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 junit.framework.AssertionFailedError;
24 import junit.framework.TestCase;
25 
26 /** Unit test for {@link com.google.common.base.Verify}. */
27 @GwtCompatible(emulated = true)
28 public class VerifyTest extends TestCase {
testVerify_simple_success()29   public void testVerify_simple_success() {
30     verify(true);
31   }
32 
testVerify_simple_failure()33   public void testVerify_simple_failure() {
34     try {
35       verify(false);
36       fail();
37     } catch (VerifyException expected) {
38     }
39   }
40 
testVerify_simpleMessage_success()41   public void testVerify_simpleMessage_success() {
42     verify(true, "message");
43   }
44 
testVerify_simpleMessage_failure()45   public void testVerify_simpleMessage_failure() {
46     try {
47       verify(false, "message");
48       fail();
49     } catch (VerifyException expected) {
50       assertThat(expected).hasMessageThat().isEqualTo("message");
51     }
52   }
53 
testVerify_complexMessage_success()54   public void testVerify_complexMessage_success() {
55     verify(true, "%s", IGNORE_ME);
56   }
57 
testVerify_complexMessage_failure()58   public void testVerify_complexMessage_failure() {
59     try {
60       verify(false, FORMAT, 5);
61       fail();
62     } catch (VerifyException expected) {
63       checkMessage(expected);
64     }
65   }
66 
67   private static final String NON_NULL_STRING = "foo";
68 
testVerifyNotNull_simple_success()69   public void testVerifyNotNull_simple_success() {
70     String result = verifyNotNull(NON_NULL_STRING);
71     assertSame(NON_NULL_STRING, result);
72   }
73 
testVerifyNotNull_simple_failure()74   public void testVerifyNotNull_simple_failure() {
75     try {
76       verifyNotNull(null);
77       fail();
78     } catch (VerifyException expected) {
79     }
80   }
81 
testVerifyNotNull_complexMessage_success()82   public void testVerifyNotNull_complexMessage_success() {
83     String result = verifyNotNull(NON_NULL_STRING, "%s", IGNORE_ME);
84     assertSame(NON_NULL_STRING, result);
85   }
86 
testVerifyNotNull_simpleMessage_failure()87   public void testVerifyNotNull_simpleMessage_failure() {
88     try {
89       verifyNotNull(null, FORMAT, 5);
90       fail();
91     } catch (VerifyException expected) {
92       checkMessage(expected);
93     }
94   }
95 
96   @GwtIncompatible // NullPointerTester
testNullPointers()97   public void testNullPointers() {
98     // Don't bother testing: Verify is like Preconditions. See the discussion on that class.
99   }
100 
101   private static final Object IGNORE_ME =
102       new Object() {
103         @Override
104         public String toString() {
105           throw new AssertionFailedError();
106         }
107       };
108 
109   private static final String FORMAT = "I ate %s pies.";
110 
checkMessage(Exception e)111   private static void checkMessage(Exception e) {
112     assertThat(e).hasMessageThat().isEqualTo("I ate 5 pies.");
113   }
114 }
115