Three types of Test
- Unit test: testing the smallest unit of your code
- Integration test: test an entire component or subsystem
- Functional test: end-to-end functionality
Testing Java applications
To execute tests, you'll need to compile test code first. Optimally, you'll have a dedicated directory to hold the source code, and another to hold compiled test classes.
Project layout
src/test/java
src/test/resources
You can also find test results XML under build/test-results. Many test framework will transform them into reports, which can be found under build/reports/test. You need to add dependency if you want to use these frameworks, such as JUnit or TestNG.
Test configurations
There are two new configurations that can be used to declare dependencies for test code compilation or execution: testCompile and testRuntime.
An example declares a compile-time dependency JUnit.
dependencies {
testCompile 'junit:junit:4.11'
}
Another configuration testRuntime is used for dependencies not needed for compilation but needed at runtime during execution.
Keep in mind that dependencies
assigned to test configurations don’t influence the classpath of your production code.
In other words, they’re not used for the compilation or packaging process.
However, the test configurations extend from specific configuration to handle dependencies which are needed for production code.
Test tasks
compileJava -> processResources -> classes -> jar -> compileTestJava -> processTestResources -> testClasses -> test
Tasks about test may be UP-TO-DATE to skip if there is no test source code.
And if you run 'gradle jar', the test tasks will also be skipped.
Test detection
How does Gradle figure out which classes to run?
Compiled test classes are in build/classes/test. Classes matching the following descriptions are inspected:
- Any class or superclass that extends junit.framework.TestCase or groovy.util.GroovyTestCase
- Any class or superclass annotated with @RunWith
- Any class or superclass that contains one method annotated with @Test
Unit test
You can use JUnit or TestNG, or Spock.
Using JUnit
Adding the dependency
project(':repository') {
repositories {
mavenCentral()
}
dependencies {
compile project(':model')
testCompile 'junit:junit:4.11'
}
}
'testCompile' means JUnit can be used to compile the test source files.
Writing unit tests
public class InMemoryToDoRepositoryTest {
private ToDoRepository inMemoryToDoRepository;
@Before
public void setUp() {
inMemoryToDoRepository = new InMemoryToDoRepository();
}
@Test
public void insertToDoItem() {
ToDoItem newToDoItem = new ToDoItem();
newToDoItem.setName("Write unit tests");
Long newId = inMemoryToDoRepository.insert(newToDoItem);
ToDoItem persistedToDoItem = inMemoryToDoRepository.findById(newId);
assertNotNull(persistedToDoItem);
assertEquals(newToDoItem, persistedToDoItem);
}
}
Executing tests
The task test will first compile the production source, and then create the JAR file followed by test sources compilation and test execution.
Using other test frameworks
Using TestNG
You need to
- add dependency
- Specify that TestNG should be used to execute tests by calling the method test.useTestNG().
The build script will look like this
apply plugin: 'groovy'
dependencies {
compile project(':model')
testCompile group: 'org.testng', name: 'testng', version: '6.8'
}
test.useTestNG()
repositories {
mavenCentral()
}
JUnit tests will not run anymore.
Using both JUnit and TestNG
Defining Test Tasks
We can add a new task of class type "Test" named "TestNG"
task TestNG (type: Test) {
useTestNG()
}
test.dependsOn TestNG
In this way, every time we run gradle test will run task TestNG firstly, which enables JUnit and TestNG running simultaneously.
网友评论