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