From: David S. <ds...@us...> - 2007-07-02 18:11:44
|
Update of /cvsroot/junit/junit/src/org/junit/tests In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv27902/src/org/junit/tests Modified Files: UserStopTest.java AllTests.java AssertionTest.java Added Files: AssumptionTest.java Log Message: - The hamcrest-core-1.1 library is now included in the JUnit distribution. For more hamcrest matchers, see the hamcrest-library jar from http://code.google.com/p/hamcrest - The Popper Theory runner (http://popper.tigris.org) has been absorbed into the JUnit project, under the package name org.junit.experimental.theories - Several additional libraries used in the theories tests have been added in a new testlib directory - New "assertThat" statement to work with hamcrest matchers: // same as assertEquals(3, x) assertThat(x, is(3)); // same as assertNull(y) assertThat(y, nullValue()); - New feature: assumeThat. A failed assumption will cause the test to pass, without further execution. (The behavior of assumeThat may change in the future to allow richer reporting of tests that are skipped because of failed assumptions) // pass on any non-Windows system @Test public void getRootDrive() { assumeThat(getOsString(), is("Windows")); getFile("C:\"); // ... } - Convenience assumption functions: // none of these are null assumeNotNull(a, b, c); assumeTrue(everythingOk()); try { getDatabaseConnection(); } catch (Exception e) { assumeNoException(e); } - Documentation fixed for many assertEquals array methods - Two bugs in numeric equality fixed: 1718905: assertEquals does not compare float correctly 1715326: assertEquals does not compare java.math.BigDecimal properly - The protocol for overriding JUnit4ClassRunner has changed again. Please see the source for details. - Extenders can now extend TestMethod to describe the behavior of running methods that do not have a @Test annotation. - Adding Annotations to Description caused a binary compatibility problem with clients compiled on previous JUnit versions. This has been fixed. --- NEW FILE: AssumptionTest.java --- package org.junit.tests; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.Matchers.is; import static org.junit.Assert.*; import static org.junit.Assume.assumeNoException; import static org.junit.Assume.assumeNotNull; import static org.junit.Assume.assumeThat; import org.hamcrest.Matchers; import org.junit.Assume; import org.junit.Test; import org.junit.Assume.AssumptionViolatedException; import org.junit.runner.JUnitCore; import org.junit.runner.Result; public class AssumptionTest { public static class HasFailingAssumption { @Test public void assumptionsFail() { assumeThat(3, is(4)); fail(); } } @Test public void failedAssumptionsMeanIgnore() { Result result= JUnitCore.runClasses(HasFailingAssumption.class); assertThat(result.getRunCount(), is(1)); assertThat(result.getIgnoreCount(), is(0)); assertThat(result.getFailureCount(), is(0)); } public static class HasPassingAssumption { @Test public void assumptionsFail() { assumeThat(3, is(3)); fail(); } } @Test public void passingAssumptionsScootThrough() { Result result= JUnitCore.runClasses(HasPassingAssumption.class); assertThat(result.getRunCount(), is(1)); assertThat(result.getIgnoreCount(), is(0)); assertThat(result.getFailureCount(), is(1)); } @Test(expected= AssumptionViolatedException.class) public void assumeThatWorks() { assumeThat(1, is(2)); } @Test public void assumeThatPasses() { assumeThat(1, is(1)); assertCompletesNormally(); } @Test public void assumeThatPassesOnStrings() { assumeThat("x", is("x")); assertCompletesNormally(); } @Test(expected= AssumptionViolatedException.class) public void assumeNotNullThrowsException() { Object[] objects= { 1, 2, null }; assumeNotNull(objects); } @Test public void assumeNotNullPasses() { Object[] objects= { 1, 2 }; assumeNotNull(objects); assertCompletesNormally(); } @Test public void assumeNotNullIncludesParameterList() { try { Object[] objects= { 1, 2, null }; assumeNotNull(objects); } catch (AssumptionViolatedException e) { assertThat(e.getMessage(), Matchers.containsString("1, 2, null")); } catch (Exception e) { fail("Should have thrown AssumptionViolatedException"); } } @Test public void assumeNoExceptionThrows() { final Throwable exception= new NullPointerException(); try { assumeNoException(exception); fail("Should have thrown exception"); } catch (AssumptionViolatedException e) { assertThat(e.getCause(), is(exception)); } } private void assertCompletesNormally() { } @Test(expected=AssumptionViolatedException.class) public void assumeTrueWorks() { Assume.assumeTrue(false); } } Index: UserStopTest.java =================================================================== RCS file: /cvsroot/junit/junit/src/org/junit/tests/UserStopTest.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- UserStopTest.java 20 Mar 2007 14:43:49 -0000 1.2 +++ UserStopTest.java 2 Jul 2007 18:11:09 -0000 1.3 @@ -17,13 +17,6 @@ @Test(expected=StoppedByUserException.class) public void userStop() { fNotifier.fireTestStarted(null); } - - - // TODO: API is changing -// @Test(expected=StoppedByUserException.class) public void stopMethodRunner() throws Exception { -// new TestMethodRunner(this, OneTest.class.getMethod("foo"), fNotifier, -// Description.createTestDescription(OneTest.class, "foo")).run(); -// } public static class OneTest { @Test public void foo() {} Index: AllTests.java =================================================================== RCS file: /cvsroot/junit/junit/src/org/junit/tests/AllTests.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- AllTests.java 26 Apr 2007 19:56:30 -0000 1.11 +++ AllTests.java 2 Jul 2007 18:11:09 -0000 1.12 @@ -11,10 +11,10 @@ @RunWith(Suite.class) @SuiteClasses({ + AssumptionTest.class, ClassRequestTest.class, ListenerTest.class, FailedConstructionTest.class, - // TODO: What did CVS do with this? CustomRunnerTest.class, TestDescriptionTest.class, SuiteDescriptionTest.class, AllTestsTest.class, Index: AssertionTest.java =================================================================== RCS file: /cvsroot/junit/junit/src/org/junit/tests/AssertionTest.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- AssertionTest.java 26 Apr 2007 19:56:30 -0000 1.7 +++ AssertionTest.java 2 Jul 2007 18:11:09 -0000 1.8 @@ -4,7 +4,12 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; +import static org.hamcrest.CoreMatchers.equalTo; + +import java.math.BigDecimal; + import org.junit.Assert; import org.junit.ComparisonFailure; import org.junit.Test; @@ -118,11 +123,9 @@ assertArrayEquals(new long[] {1}, new long[] {1}); } - @Test - public void arraysContainingIntsAreEqualToArraysContainingLongs() { - Object[] int1= { 1, 2, 3 }; - Object[] int2= { 1L, 2L, 3L }; - assertArrayEquals(int1, int2); + @Test(expected=AssertionError.class) + public void IntegerDoesNotEqualLong() { + assertEquals(new Integer(1), new Long(1)); } @Test public void intsEqualLongs() { @@ -259,6 +262,15 @@ assertEquals(1.0, 2.0, 0.9); } + @Test(expected= AssertionError.class) public void floatsNotEqualWithoutDelta() { + assertEquals(1.0, 1.1); + } + + @Test(expected= AssertionError.class) public void bigDecimalsNotEqual() { + assertEquals(new BigDecimal("123.4"), new BigDecimal("123.0")); + } + + @Test(expected= AssertionError.class) public void doublesNotEqual() { assertEquals(1.0d, 2.0d, 0.9d); } @@ -384,4 +396,30 @@ assertEquals("expected: java.lang.String<4> but was: java.lang.Integer<4>", e.getMessage()); } } + + @Test public void assertThatIncludesDescriptionOfTestedValueInErrorMessage() { + String expected = "expected"; + String actual = "actual"; + + String expectedMessage = "identifier\nExpected: \"expected\"\n got: \"actual\"\n"; + + try { + assertThat("identifier", actual, equalTo(expected)); + } catch (AssertionError e) { + assertEquals(expectedMessage, e.getMessage()); + } + } + + @Test public void assertThatDescriptionCanBeElided() { + String expected = "expected"; + String actual = "actual"; + + String expectedMessage = "\nExpected: \"expected\"\n got: \"actual\"\n"; + + try { + assertThat(actual, equalTo(expected)); + } catch (AssertionError e) { + assertEquals(expectedMessage, e.getMessage()); + } + } } |