Skip to content

Commit

Permalink
Replace usages of @test(expected) with Assert.assertThrows().
Browse files Browse the repository at this point in the history
  • Loading branch information
Desislav-Petrov authored and motlin committed May 17, 2024
1 parent 34866aa commit b9b786a
Show file tree
Hide file tree
Showing 21 changed files with 112 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public class Unmodifiable<name>BagTest extends AbstractMutable<name>BagTestCase
}

@Override
@Test(expected = UnsupportedOperationException.class)
@Test
public void addOccurrences_throws()
{
this.newWith().addOccurrences(<(literal.(type))("1")>, -1);
assertThrows(UnsupportedOperationException.class, () -> this.newWith().addOccurrences(<(literal.(type))("1")>, -1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package org.eclipse.collections.impl.collection.mutable.primitive;
import java.util.NoSuchElementException;

import org.eclipse.collections.api.collection.primitive.Mutable<name>Collection;
import org.eclipse.collections.api.iterator.<name>Iterator;
import org.eclipse.collections.api.iterator.Mutable<name>Iterator;
import org.eclipse.collections.impl.bag.mutable.primitive.<name>HashBag;
import org.eclipse.collections.impl.block.factory.primitive.<name>Predicates;
Expand Down Expand Up @@ -398,23 +397,6 @@ public abstract class AbstractMutable<name>CollectionTestCase extends Abstract<n
Assert.assertEquals(unmodifiableCollection, collection.asUnmodifiable());
}

@Override
@Test(expected = NoSuchElementException.class)
public void <type>Iterator_throws_non_empty_collection()
{
super.<type>Iterator_throws_non_empty_collection();
Mutable<name>Collection collection = this.newWith();
collection.add(<(literal.(type))("1")>);
collection.add(<(literal.(type))("2")>);
collection.add(<(literal.(type))("3")>);
<name>Iterator iterator = collection.<type>Iterator();
while (iterator.hasNext())
{
iterator.next();
}
iterator.next();
}

@Test
public void <type>Iterator_with_remove()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertThrows;

/**
* Abstract JUnit test for {@link <name>Iterable}s
* This file was automatically generated from template file abstractPrimitiveIterableTestCase.stg.
Expand Down Expand Up @@ -447,18 +449,18 @@ public abstract class Abstract<name>IterableTestCase
@Test
public abstract void <type>Iterator();

@Test(expected = NoSuchElementException.class)
@Test
public void <type>Iterator_throws()
{
<name>Iterator iterator = this.classUnderTest().<type>Iterator();
while (iterator.hasNext())
{
iterator.next();
}
iterator.next();
assertThrows(NoSuchElementException.class, () -> iterator.next());
}

@Test(expected = NoSuchElementException.class)
@Test
public void <type>Iterator_throws_non_empty_collection()
{
<name>Iterable iterable = this.newWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">);
Expand All @@ -467,7 +469,7 @@ public abstract class Abstract<name>IterableTestCase
{
iterator.next();
}
iterator.next();
assertThrows(NoSuchElementException.class, () -> iterator.next());
}

@Test
Expand Down Expand Up @@ -742,10 +744,10 @@ public abstract class Abstract<name>IterableTestCase
<(maxTests.(type))(type, name)>
}

@Test(expected = NoSuchElementException.class)
@Test
public void max_throws_emptyCollection()
{
this.newWith().max();
assertThrows(NoSuchElementException.class, () -> this.newWith().max());
}

@Test
Expand All @@ -754,10 +756,10 @@ public abstract class Abstract<name>IterableTestCase
<(minTests.(type))(type, name)>
}

@Test(expected = NoSuchElementException.class)
@Test
public void min_throws_emptyCollection()
{
this.newWith().min();
assertThrows(NoSuchElementException.class, () -> this.newWith().min());
}

@Test
Expand Down Expand Up @@ -845,10 +847,10 @@ public void sumConsistentRounding()
Assert.assertEquals(31.0, this.newWith(<["30", "30", "31", "31", "32", "32"]:(literal.(type))(); separator=", ">).average(), 0.0);
}

@Test(expected = ArithmeticException.class)
@Test
public void averageThrowsOnEmpty()
{
this.newWith().average();
assertThrows(ArithmeticException.class, () -> this.newWith().average());
}

/**
Expand Down Expand Up @@ -877,10 +879,10 @@ public void sumConsistentRounding()
Assert.assertEquals(30.5, this.newWith(<["1", "30", "30", "31", "31", "32"]:(literal.(type))(); separator=", ">).median(), 0.0);
}

@Test(expected = ArithmeticException.class)
@Test
public void medianThrowsOnEmpty()
{
this.newWith().median();
assertThrows(ArithmeticException.class, () -> this.newWith().median());
}

/**
Expand Down Expand Up @@ -1307,10 +1309,12 @@ public void sumConsistentRounding()

<injectIntoPrimitiveTest("Double", "double", true)>

@Test(expected = NoSuchElementException.class)
@Test
public void reduceOnEmptyThrows()
{
this.newWith().reduce((<(wideType.(type))> result, <type> value) -> <(castWideType.(type))>result + <(castWideType.(type))>value + <(wideLiteral.(type))("1")>);
assertThrows(
NoSuchElementException.class,
() -> this.newWith().reduce((<(wideType.(type))> result, <type> value) -> <(castWideType.(type))>result + <(castWideType.(type))>value + <(wideLiteral.(type))("1")>));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertThrows;

/**
* JUnit test for {@link Synchronized<name>Iterable}s
* This file was automatically generated from template file synchronizedPrimitiveIterableTest.stg.
Expand Down Expand Up @@ -62,10 +64,10 @@ public class Synchronized<name>IterableTest extends Abstract<name>IterableTestCa
return FastList.newListWith(elements);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void null_iterable_throws()
{
Synchronized<name>Iterable.of(null);
assertThrows(IllegalArgumentException.class, () -> Synchronized<name>Iterable.of(null));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ public abstract class AbstractLazy<name>IterableTestCase
Assert.assertEquals(<(wideLiteral.(type))("33")>, this.newWith(<["0", "33"]:(literal.(type))(); separator=", ">).sum()<(wideDelta.(type))>);
}

@Test(expected = NoSuchElementException.class)
@Test
public void max_throws_emptyIterable()
{
this.getEmptyIterable().max();
assertThrows(NoSuchElementException.class, () -> this.getEmptyIterable().max());
}

@Test
Expand Down Expand Up @@ -282,10 +282,10 @@ public abstract class AbstractLazy<name>IterableTestCase
this.classUnderTest().select(<name>Predicates.lessThan(<(literal.(type))("0")>)).maxIfEmpty(<(literal.(type))("0")>)<(wideDelta.(type))>);
}

@Test(expected = NoSuchElementException.class)
@Test
public void maxThrowsOnEmpty()
{
new Lazy<name>IterableAdapter(new <name>ArrayList()).max();
assertThrows(NoSuchElementException.class, () -> new Lazy<name>IterableAdapter(new <name>ArrayList()).max());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,11 @@ public void sumConsistentRounding()
Assert.assertEquals(4.0, Interval.oneTo(7).collect<name>(PrimitiveFunctions.unboxIntegerTo<name>()).median(), 0.001);
}

@Test(expected = ArithmeticException.class)
@Test
public void medianThrowsOnEmpty()
{
Lists.mutable.\<Integer>of().asLazy().collect<name>(PrimitiveFunctions.unboxIntegerTo<name>()).median();
assertThrows(ArithmeticException.class,
() -> Lists.mutable.\<Integer>of().asLazy().collect<name>(PrimitiveFunctions.unboxIntegerTo<name>()).median());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,12 @@ public void sumConsistentRounding()
new Select<name>Iterable(new <name>ArrayList(), <name>Predicates.lessThan(<(literal.(type))("3")>)).max());
}

@Test(expected = NoSuchElementException.class)
@Test
public void minThrowsOnEmpty()
{
new Select<name>Iterable(new <name>ArrayList(), <name>Predicates.lessThan(<(literal.(type))("3")>)).min();
assertThrows(
NoSuchElementException.class,
() -> new Select<name>Iterable(new <name>ArrayList(), <name>Predicates.lessThan(<(literal.(type))("3")>)).min());
}

@Test
Expand All @@ -239,10 +241,11 @@ public void sumConsistentRounding()
Assert.assertEquals(1.5d, this.iterable.median(), 0.0);
}

@Test(expected = ArithmeticException.class)
@Test
public void medianThrowsOnEmpty()
{
new Select<name>Iterable(new <name>ArrayList(), <name>Predicates.lessThan(<(literal.(type))("3")>)).median();
assertThrows(ArithmeticException.class,
() -> new Select<name>Iterable(new <name>ArrayList(), <name>Predicates.lessThan(<(literal.(type))("3")>)).median());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ public class Tap<name>IterableTest
Assert.assertEquals(list.makeString(""), builder.toString());
}

@Test(expected = ArithmeticException.class)
@Test
public void medianThrowsOnEmpty()
{
new Tap<name>Iterable(new <name>ArrayList(), System.out::println).median();
assertThrows(ArithmeticException.class, () -> new Tap<name>Iterable(new <name>ArrayList(), System.out::println).median());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ public class Reverse<name>IterableTest
Assert.assertEquals(<(wideLiteral.(type))("9")>, <name>ArrayList.newListWith(<["1", "0", "9", "7"]:(literal.(type))(); separator=", ">).asReversed().max()<(wideDelta.(type))>);
}

@Test(expected = NoSuchElementException.class)
@Test
public void max_throws_emptyList()
{
new <name>ArrayList().asReversed().max();
assertThrows(NoSuchElementException.class, () -> new <name>ArrayList().asReversed().max());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.Collections;<endif>

import static org.junit.Assert.assertThrows;

/**
* Abstract JUnit test for {@link Immutable<name>List}.
Expand Down Expand Up @@ -80,16 +81,16 @@ public abstract class AbstractImmutable<name>ListTestCase extends AbstractImmuta
}
}

@Test(expected = IndexOutOfBoundsException.class)
@Test
public void get_throws_index_greater_than_size()
{
this.classUnderTest().get(this.classUnderTest().size());
assertThrows(IndexOutOfBoundsException.class, () -> this.classUnderTest().get(this.classUnderTest().size()));
}

@Test(expected = IndexOutOfBoundsException.class)
@Test
public void get_throws_index_negative()
{
this.classUnderTest().get(-1);
assertThrows(IndexOutOfBoundsException.class, () -> this.classUnderTest().get(-1));
}

@Test
Expand Down Expand Up @@ -149,10 +150,10 @@ public abstract class AbstractImmutable<name>ListTestCase extends AbstractImmuta
Assert.assertFalse(iterator.hasNext());
}

@Test(expected = UnsupportedOperationException.class)
@Test
public void subList()
{
this.classUnderTest().subList(0, 1);
Assert.assertThrows(UnsupportedOperationException.class, () -> this.classUnderTest().subList(0, 1));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import org.eclipse.collections.impl.test.Verify;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertThrows;

/**
* JUnit test for {@link Immutable<name>ArrayList}.
* This file was automatically generated from template file immutablePrimitiveArrayListTest.stg.
Expand All @@ -46,16 +48,16 @@ public class Immutable<name>ArrayListTest extends AbstractImmutable<name>ListTes
Assert.assertEquals(<name>ArrayList.newListWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">), Immutable<name>ArrayList.newList(<name>ArrayList.newListWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">)));
}

@Test(expected = IllegalArgumentException.class)
@Test
public void newCollection_throws_empty()
{
Immutable<name>ArrayList.newListWith();
assertThrows(IllegalArgumentException.class, () -> Immutable<name>ArrayList.newListWith());
}

@Test(expected = IllegalArgumentException.class)
@Test
public void newCollection_throws_single()
{
Immutable<name>ArrayList.newListWith(<(literal.(type))("42")>);
assertThrows(IllegalArgumentException.class, () -> Immutable<name>ArrayList.newListWith(<(literal.(type))("42")>));
}

@Override
Expand All @@ -74,12 +76,12 @@ public class Immutable<name>ArrayListTest extends AbstractImmutable<name>ListTes
Assert.assertEquals(<(wideLiteral.(type))("14")>, list1.dotProduct(list2)<(wideDelta.(type))>);
}

@Test(expected = IllegalArgumentException.class)
@Test
public void dotProduct_throwsOnListsOfDifferentSizes()
{
Immutable<name>ArrayList list1 = Immutable<name>ArrayList.newListWith(<["1", "2", "3"]:(literal.(type))(); separator=", ">);
Immutable<name>ArrayList list2 = Immutable<name>ArrayList.newListWith(<["1", "2"]:(literal.(type))(); separator=", ">);
list1.dotProduct(list2);
assertThrows(IllegalArgumentException.class, () -> list1.dotProduct(list2));
}

@Test
Expand Down

0 comments on commit b9b786a

Please sign in to comment.