JUnit4 allows use of expected exceptions

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1388108 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sebastian Bazley 2012-09-20 16:36:57 +00:00
parent f25a596ae9
commit 247cf8c4dc
10 changed files with 116 additions and 134 deletions

View File

@ -156,15 +156,10 @@ public class BackgroundInitializerTest {
/** /**
* Tests calling get() before start(). This should cause an exception. * Tests calling get() before start(). This should cause an exception.
*/ */
@Test @Test(expected=IllegalStateException.class)
public void testGetBeforeStart() throws ConcurrentException { public void testGetBeforeStart() throws ConcurrentException {
BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl(); BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl();
try { init.get();
init.get();
fail("Could call get() before start()!");
} catch (IllegalStateException istex) {
// ok
}
} }
/** /**

View File

@ -43,34 +43,18 @@ import org.junit.Test;
*/ */
public class EventListenerSupportTest public class EventListenerSupportTest
{ {
@Test @Test(expected=NullPointerException.class)
public void testAddNullListener() public void testAddNullListener()
{ {
EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
try listenerSupport.addListener(null);
{
listenerSupport.addListener(null);
fail("Should not be able to add a null listener.");
}
catch (NullPointerException e)
{
}
} }
@Test @Test(expected=NullPointerException.class)
public void testRemoveNullListener() public void testRemoveNullListener()
{ {
EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class); EventListenerSupport<VetoableChangeListener> listenerSupport = EventListenerSupport.create(VetoableChangeListener.class);
try listenerSupport.removeListener(null);
{
listenerSupport.removeListener(null);
fail("Should not be able to remove a null listener.");
}
catch (NullPointerException e)
{
}
} }
@Test @Test
@ -89,32 +73,16 @@ public class EventListenerSupportTest
assertSame(calledListeners.get(1), listener2); assertSame(calledListeners.get(1), listener2);
} }
@Test @Test(expected=IllegalArgumentException.class)
public void testCreateWithNonInterfaceParameter() public void testCreateWithNonInterfaceParameter()
{ {
try EventListenerSupport.create(String.class);
{
EventListenerSupport.create(String.class);
fail("Should not be able to create using non-interface class.");
}
catch (IllegalArgumentException e)
{
}
} }
@Test @Test(expected=NullPointerException.class)
public void testCreateWithNullParameter() public void testCreateWithNullParameter()
{ {
try EventListenerSupport.create(null);
{
EventListenerSupport.create(null);
fail("Should not be able to create using null class.");
}
catch (NullPointerException e)
{
}
} }
@Test @Test

View File

@ -452,13 +452,9 @@ public class ExceptionUtilsTest {
assertFalse(match); assertFalse(match);
} }
@Test @Test(expected=IllegalArgumentException.class)
public void testRemoveCommonFrames_ListList() throws Exception { public void testRemoveCommonFrames_ListList() throws Exception {
try { ExceptionUtils.removeCommonFrames(null, null);
ExceptionUtils.removeCommonFrames(null, null);
fail();
} catch (IllegalArgumentException ex) {
}
} }
@Test @Test

View File

@ -430,12 +430,9 @@ public class FractionTest {
} }
} }
@Test @Test(expected=IllegalArgumentException.class)
public void testFactory_String() { public void testFactory_String() {
try { Fraction.getFraction(null);
Fraction.getFraction(null);
fail("expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {}
} }

View File

@ -38,12 +38,12 @@ public class MutableBooleanTest {
mutBool.setValue(true); mutBool.setValue(true);
assertEquals(+1, mutBool.compareTo(new MutableBoolean(false))); assertEquals(+1, mutBool.compareTo(new MutableBoolean(false)));
assertEquals(0, mutBool.compareTo(new MutableBoolean(true))); assertEquals(0, mutBool.compareTo(new MutableBoolean(true)));
}
try {
mutBool.compareTo(null); @Test(expected=NullPointerException.class)
fail(); public void testCompareToNull() {
} catch (NullPointerException ex) { final MutableBoolean mutBool = new MutableBoolean(false);
} mutBool.compareTo(null);
} }
// ---------------------------------------------------------------- // ----------------------------------------------------------------
@ -57,11 +57,11 @@ public class MutableBooleanTest {
assertTrue(new MutableBoolean(Boolean.TRUE).booleanValue()); assertTrue(new MutableBoolean(Boolean.TRUE).booleanValue());
assertFalse(new MutableBoolean(Boolean.FALSE).booleanValue()); assertFalse(new MutableBoolean(Boolean.FALSE).booleanValue());
try { }
new MutableBoolean(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) { public void testConstructorNull() {
} new MutableBoolean(null);
} }
@Test @Test
@ -105,11 +105,12 @@ public class MutableBooleanTest {
mutBool.setValue(true); mutBool.setValue(true);
assertTrue(mutBool.booleanValue()); assertTrue(mutBool.booleanValue());
try { }
mutBool.setValue(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) { public void testSetNull() {
} final MutableBoolean mutBool = new MutableBoolean(false);
mutBool.setValue(null);
} }
@Test @Test

View File

@ -39,10 +39,11 @@ public class MutableByteTest {
assertEquals((byte) 2, new MutableByte("2").byteValue()); assertEquals((byte) 2, new MutableByte("2").byteValue());
try { }
new MutableByte((Number)null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testConstructorNull() {
new MutableByte((Number)null);
} }
@Test @Test
@ -62,10 +63,12 @@ public class MutableByteTest {
mutNum.setValue(new MutableByte((byte) 3)); mutNum.setValue(new MutableByte((byte) 3));
assertEquals((byte) 3, mutNum.byteValue()); assertEquals((byte) 3, mutNum.byteValue());
assertEquals(Byte.valueOf((byte) 3), mutNum.getValue()); assertEquals(Byte.valueOf((byte) 3), mutNum.getValue());
try { }
mutNum.setValue(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testSetNull() {
final MutableByte mutNum = new MutableByte((byte) 0);
mutNum.setValue(null);
} }
@Test @Test
@ -105,10 +108,12 @@ public class MutableByteTest {
assertEquals((byte) 0, mutNum.compareTo(new MutableByte((byte) 0))); assertEquals((byte) 0, mutNum.compareTo(new MutableByte((byte) 0)));
assertEquals((byte) +1, mutNum.compareTo(new MutableByte((byte) -1))); assertEquals((byte) +1, mutNum.compareTo(new MutableByte((byte) -1)));
assertEquals((byte) -1, mutNum.compareTo(new MutableByte((byte) 1))); assertEquals((byte) -1, mutNum.compareTo(new MutableByte((byte) 1)));
try { }
mutNum.compareTo(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testCompareToNull() {
final MutableByte mutNum = new MutableByte((byte) 0);
mutNum.compareTo(null);
} }
@Test @Test

View File

@ -39,10 +39,11 @@ public class MutableDoubleTest {
assertEquals(2d, new MutableDouble("2.0").doubleValue(), 0.0001d); assertEquals(2d, new MutableDouble("2.0").doubleValue(), 0.0001d);
try { }
new MutableDouble((Number)null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testConstructorNull() {
new MutableDouble((Number)null);
} }
@Test @Test
@ -62,10 +63,12 @@ public class MutableDoubleTest {
mutNum.setValue(new MutableDouble(3d)); mutNum.setValue(new MutableDouble(3d));
assertEquals(3d, mutNum.doubleValue(), 0.0001d); assertEquals(3d, mutNum.doubleValue(), 0.0001d);
assertEquals(Double.valueOf(3d), mutNum.getValue()); assertEquals(Double.valueOf(3d), mutNum.getValue());
try { }
mutNum.setValue(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testSetNull() {
final MutableDouble mutNum = new MutableDouble(0d);
mutNum.setValue(null);
} }
@Test @Test
@ -117,10 +120,12 @@ public class MutableDoubleTest {
assertEquals(0, mutNum.compareTo(new MutableDouble(0d))); assertEquals(0, mutNum.compareTo(new MutableDouble(0d)));
assertEquals(+1, mutNum.compareTo(new MutableDouble(-1d))); assertEquals(+1, mutNum.compareTo(new MutableDouble(-1d)));
assertEquals(-1, mutNum.compareTo(new MutableDouble(1d))); assertEquals(-1, mutNum.compareTo(new MutableDouble(1d)));
try { }
mutNum.compareTo(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testCompareToNull() {
final MutableDouble mutNum = new MutableDouble(0d);
mutNum.compareTo(null);
} }
@Test @Test

View File

@ -39,10 +39,11 @@ public class MutableFloatTest {
assertEquals(2f, new MutableFloat("2.0").floatValue(), 0.0001f); assertEquals(2f, new MutableFloat("2.0").floatValue(), 0.0001f);
try { }
new MutableFloat((Number)null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testConstructorNull() {
new MutableFloat((Number)null);
} }
@Test @Test
@ -62,10 +63,12 @@ public class MutableFloatTest {
mutNum.setValue(new MutableFloat(3f)); mutNum.setValue(new MutableFloat(3f));
assertEquals(3f, mutNum.floatValue(), 0.0001f); assertEquals(3f, mutNum.floatValue(), 0.0001f);
assertEquals(Float.valueOf(3f), mutNum.getValue()); assertEquals(Float.valueOf(3f), mutNum.getValue());
try { }
mutNum.setValue(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testSetNull() {
final MutableFloat mutNum = new MutableFloat(0f);
mutNum.setValue(null);
} }
@Test @Test
@ -117,10 +120,12 @@ public class MutableFloatTest {
assertEquals(0, mutNum.compareTo(new MutableFloat(0f))); assertEquals(0, mutNum.compareTo(new MutableFloat(0f)));
assertEquals(+1, mutNum.compareTo(new MutableFloat(-1f))); assertEquals(+1, mutNum.compareTo(new MutableFloat(-1f)));
assertEquals(-1, mutNum.compareTo(new MutableFloat(1f))); assertEquals(-1, mutNum.compareTo(new MutableFloat(1f)));
try { }
mutNum.compareTo(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testCompareToNull() {
final MutableFloat mutNum = new MutableFloat(0f);
mutNum.compareTo(null);
} }
@Test @Test

View File

@ -39,10 +39,11 @@ public class MutableIntTest {
assertEquals(2, new MutableInt("2").intValue()); assertEquals(2, new MutableInt("2").intValue());
try { }
new MutableInt((Number)null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testConstructorNull() {
new MutableInt((Number)null);
} }
@Test @Test
@ -62,10 +63,12 @@ public class MutableIntTest {
mutNum.setValue(new MutableLong(3)); mutNum.setValue(new MutableLong(3));
assertEquals(3, mutNum.intValue()); assertEquals(3, mutNum.intValue());
assertEquals(Integer.valueOf(3), mutNum.getValue()); assertEquals(Integer.valueOf(3), mutNum.getValue());
try { }
mutNum.setValue(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testSetNull() {
final MutableInt mutNum = new MutableInt(0);
mutNum.setValue(null);
} }
@Test @Test
@ -112,10 +115,12 @@ public class MutableIntTest {
assertEquals(0, mutNum.compareTo(new MutableInt(0))); assertEquals(0, mutNum.compareTo(new MutableInt(0)));
assertEquals(+1, mutNum.compareTo(new MutableInt(-1))); assertEquals(+1, mutNum.compareTo(new MutableInt(-1)));
assertEquals(-1, mutNum.compareTo(new MutableInt(1))); assertEquals(-1, mutNum.compareTo(new MutableInt(1)));
try { }
mutNum.compareTo(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testCompareToNull() {
final MutableInt mutNum = new MutableInt(0);
mutNum.compareTo(null);
} }
@Test @Test

View File

@ -39,10 +39,11 @@ public class MutableLongTest {
assertEquals(2, new MutableLong("2").longValue()); assertEquals(2, new MutableLong("2").longValue());
try { }
new MutableLong((Number)null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testConstructorNull() {
new MutableLong((Number)null);
} }
@Test @Test
@ -62,10 +63,12 @@ public class MutableLongTest {
mutNum.setValue(new MutableLong(3)); mutNum.setValue(new MutableLong(3));
assertEquals(3, mutNum.longValue()); assertEquals(3, mutNum.longValue());
assertEquals(Long.valueOf(3), mutNum.getValue()); assertEquals(Long.valueOf(3), mutNum.getValue());
try { }
mutNum.setValue(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testSetNull() {
final MutableLong mutNum = new MutableLong(0);
mutNum.setValue(null);
} }
@Test @Test
@ -105,10 +108,12 @@ public class MutableLongTest {
assertEquals(0, mutNum.compareTo(new MutableLong(0))); assertEquals(0, mutNum.compareTo(new MutableLong(0)));
assertEquals(+1, mutNum.compareTo(new MutableLong(-1))); assertEquals(+1, mutNum.compareTo(new MutableLong(-1)));
assertEquals(-1, mutNum.compareTo(new MutableLong(1))); assertEquals(-1, mutNum.compareTo(new MutableLong(1)));
try { }
mutNum.compareTo(null);
fail(); @Test(expected=NullPointerException.class)
} catch (NullPointerException ex) {} public void testCompareToNull() {
final MutableLong mutNum = new MutableLong(0);
mutNum.compareTo(null);
} }
@Test @Test