The below examples use JUnit5.
In the simplest use of JUnit, we tag each method that represents a test with the @Test annotation. JUnit identifies these methods as individual tests and sets up and runs them one at a time, taking measures to avoid side effects between tests.
// validating/CountedList.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Keeps track of how many of itself are created.
package validating;
import java.util.*;
public class CountedList extends ArrayList<String> {
private static int counter = 0;
private int id = counter++;
public CountedList() {
System.out.println("CountedList #" + id);
}
public int getId() {
return id;
}
}
// validating/tests/CountedListTest.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Simple use of JUnit to test CountedList.
package validating;
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import org.junit.jupiter.api.*;
public class CountedListTest {
private CountedList list;
@BeforeAll
static void beforeAllMsg() { // static method
System.out.println(">>> Starting CountedListTest");
}
@AfterAll
static void afterAllMsg() { // static method
System.out.println(">>> Finished CountedListTest");
}
@BeforeEach
public void initialize() {
list = new CountedList();
System.out.println("Set up for " + list.getId());
for (int i = 0; i < 3; i++) {
list.add(Integer.toString(i));
}
}
@AfterEach
public void cleanup() {
System.out.println("Cleaning up " + list.getId());
}
@Test
public void insert() {
System.out.println("Running testInsert()");
assertEquals(list.size(), 3);
list.add(1, "Insert");
assertEquals(list.size(), 4);
assertEquals(list.get(1), "Insert");
}
@Test
public void replace() {
System.out.println("Running testReplace()");
assertEquals(list.size(), 3);
list.set(1, "Replace");
assertEquals(list.size(), 3);
assertEquals(list.get(1), "Replace");
}
// A helper method to simplify the code. As
// long as it's not annotated with @Test, it will
// not be automatically executed by JUnit.
private void compare(List<String> lst, String[] strs) {
assertArrayEquals(lst.toArray(new String[0]), strs);
}
@Test
public void order() {
System.out.println("Running testOrder()");
compare(list, new String[] {"0", "1", "2"});
}
@Test
public void remove() {
System.out.println("Running testRemove()");
assertEquals(list.size(), 3);
list.remove(1);
assertEquals(list.size(), 2);
compare(list, new String[] {"0", "2"});
}
@Test
public void addAll() {
System.out.println("Running testAddAll()");
list.addAll(Arrays.asList(new String[] {"An", "African", "Swallow"}));
assertEquals(list.size(), 6);
compare(list, new String[] {"0", "1", "2", "An", "African", "Swallow"});
}
}
/* Output:
>>> Starting CountedListTest
CountedList #0
Set up for 0
Running testRemove()
Cleaning up 0
CountedList #1
Set up for 1
Running testReplace()
Cleaning up 1
CountedList #2
Set up for 2
Running testAddAll()
Cleaning up 2
CountedList #3
Set up for 3
Running testInsert()
Cleaning up 3
CountedList #4
Set up for 4
Running testOrder()
Cleaning up 4
>>> Finished CountedListTest
*/
Gradle will not re-run a task if the input and output hasn’t changed.
To rerun the tests, provide an extra argument or task if the test task is UP-TO-DATE
# Or force the tasks to be re-run # run the whole build
gradle --rerun-tasks test --tests *CountedListTestt
# Or clean the whole build before running the task
gradle clean test --tests *CountedListTest
when not add --rerun-tasks or clean, we should change Test output string, so it will run the task.
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/CountedList.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/tests/CountedListTest.java
4. https://blog.jdriven.com/2017/10/run-one-or-exclude-one-test-with-gradle/
5. https://github.com/madegroot/Run-or-Exclude-one-test-with-Gradle