Package com.google.common.testing.junit4

Source Code of com.google.common.testing.junit4.JUnitAssertsTest

/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.testing.junit4;

import static com.google.common.testing.junit4.JUnitAsserts.assertContainsRegex;
import static com.google.common.testing.junit4.JUnitAsserts.assertContentsInOrder;
import static com.google.common.testing.junit4.JUnitAsserts.assertMatchesRegex;
import static com.google.common.testing.junit4.JUnitAsserts.assertNotContainsRegex;
import static com.google.common.testing.junit4.JUnitAsserts.assertNotEqual;
import static com.google.common.testing.junit4.JUnitAsserts.assertNotMatchesRegex;

import com.google.common.testing.junit4.JUnitAsserts;

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.MatchResult;

/**
* Unit test for {@link JUnitAsserts}.
*
* @author kevinb
*/
public class JUnitAssertsTest extends TestCase {

  public void testNotEqualSuccess() {
    assertNotEqual(1, 2);
  }

  public void testNotEqualNullSuccess1() {
    assertNotEqual(null, 2);
  }

  public void testNotEqualNullSuccess2() {
    assertNotEqual(1, null);
  }

  public void testNotEqualFailure() {
    try {
      assertNotEqual(1, 1);
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("expected not to be:<1>", e.getMessage());
    }
  }

  public void testNotEqualNullFailure() {
    try {
      assertNotEqual(null, null);
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "expected not to be:<null>", e.getMessage());
    }
  }

  public void testMessageReturnedByNotEqualFailure() {
    try {
      assertNotEqual("foo", 1, 1);
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "foo expected not to be:<1>", e.getMessage());
    }
  }

  public void testMatchesRegexSuccess() {
    MatchResult result = assertMatchesRegex("a(.)", "ab");
    assertEquals("b", result.group(1));
  }

  public void testMatchesRegexFailure() {
    try {
      assertMatchesRegex("thing", "a(.)", "ace");
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "thing expected to match regex:<a(.)> but was:<ace>",
          e.getMessage());
    }
  }

  public void testMatchesRegexFailureNull() {
    try {
      assertMatchesRegex("thing", "a(.)", null);
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "thing expected to match regex:<a(.)> but was:null",
          e.getMessage());
    }
  }

  public void testContainsRegexSuccess() {
    MatchResult result = assertContainsRegex("a(.)", "ace");
    assertEquals("c", result.group(1));
  }

  public void testContainsRegexFailure() {
    try {
      assertContainsRegex("thing", "a(.)", "ha");
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "thing expected to contain regex:<a(.)> but was:<ha>",
          e.getMessage());
    }
  }

  public void testContainsRegexFailureNull() {
    try {
      assertContainsRegex("thing", "a(.)", null);
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "thing expected to contain regex:<a(.)> but was:null",
          e.getMessage());
    }
  }

  public void testNotMatchesRegexSuccess() {
    assertNotMatchesRegex("a(.)", "ace");
  }

  public void testNotMatchesRegexFailure() {
    try {
      assertNotMatchesRegex("thing", "a(.)", "ab");
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "thing expected not to match regex:<a(.)> but was:<ab>",
          e.getMessage());
    }
  }

  public void testNotContainsRegexSuccess() {
    assertNotContainsRegex("a(.)", "ha");
  }

  public void testNotContainsRegexFailure() {
    try {
      assertNotContainsRegex("thing", "a(.)", "ace");
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "thing expected not to contain regex:<a(.)> but was:<ace>",
          e.getMessage());
    }
  }

  public void testContentsInOrderSuccess() {
    List<Integer> actual = Arrays.asList(1, 2, 3);
    assertContentsInOrder(actual, 1, 2, 3);
  }

  public void testContentsInOrderEmptySuccess() {
    List<Integer> actual = Collections.emptyList();
    assertContentsInOrder(actual);
  }

  public void testContentsInOrderEmptyFailureEmptyActual() {
    List<Integer> actual = Collections.emptyList();
    try {
      assertContentsInOrder("foo", actual, 1);
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "foo expected:<[1]> but was:<[]>", e.getMessage());
    }
  }

  public void testContentsInOrderEmptyFailureEmptyExpected() {
    List<Integer> actual = Collections.singletonList(1);
    try {
      assertContentsInOrder("foo", actual);
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "foo expected:<[]> but was:<[1]>", e.getMessage());
    }
  }

  public void testContentsInOrderFailureDueToOrder() {
    List<Integer> actual = Arrays.asList(1, 2);
    try {
      assertContentsInOrder("foo", actual, 2, 1);
      fail("no exception thrown");
    } catch (AssertionFailedError e) {
      assertEquals("failure message generated by MoreAsserts",
          "foo expected:<[2, 1]> but was:<[1, 2]>", e.getMessage());
    }
  }
}
TOP

Related Classes of com.google.common.testing.junit4.JUnitAssertsTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.