A unit test targets some other "class under test;" for example, the class ArrayIntListTest might be targeting the ArrayIntList as its class under test. A unit test generally consists of various testing methods that each interact with the class under test in some specific way to make sure it works as expected. JUnit isn't part of the standard Java class libraries, but it does come included with Eclipse. Here is a direct link to download the latest JUnit v4.
To use JUnit you must create a separate. A dialog box will pop up to help you create your test case. Make sure that the option at the top is set to use JUnit 4, not JUnit 3. Click Next. You will see a set of checkboxes to indicate which methods you want to test.
Eclipse will help you by creating "stub" test methods that you can fill in. You can always add more later manually. Choose the methods to test and click Finish. At this point Eclipse will ask whether you want it to automatically attach the JUnit library to your project.
Yes, you do. If you forget to do add JUnit to your project, you can later add it to your project manually by clicking the top Project menu, then Properties, then Java Build Path, then click Add Library When you're done, you should have a nice new JUnit test case file.
I suggest that you change the second import statement at the top to say the following:. Each unit test method in your JUnit test case file should test a particular small aspect of the behavior of the "class under test. Another test might check to make sure that the list's size is correct after various manipulations.
And so on. Each testing method should be short and should test only one specific aspect of the class under test. JUnit testing methods utilize assertions , which are statements that check whether a given condition is true or false. In JUnit 4 you can use annotations to mark the following types of initializer and finalizer methods. Test Class Initializer. The BeforeClass annotation marks a method as a test class initialization method. A test class initialization method is run only once, and before any of the other methods in the test class.
For example, instead of creating a database connection in a test initializer and creating a new connection before each test method, you may want to use a test class initializer to open a connection before running the tests. You could then close the connection with the test class finalizer. Test Class Finalizer. The AfterClass annotation marks a method as a test class finalizer method.
A test class finalizer method is run only once, and after all of the other methods in the test class are finished. Test Initializer. The Before annotation marks a method as a test initialization method. A test initialization method is run before each test case in the test class.
A test initialization method is not required to run tests, but if you need to initialize some variables before you run a test, you use a test initializer method. Test Finalizer. The After annotation marks a method as a test finalizer method. A test finalizer method is run after each test case in the test class. A test finalizer method is not required to run tests, but you may need a finalizer to clean up any data that was required when running the test cases.
When you run the test class the println text you added is displayed in the output pane of the JUnit Test Results window. If you do not add the println , there is no output to indicate that the initializer and finalizer methods were run. Instead of using the generated test method testConcatWords , you will add a new test method called helloWorldCheck that uses a single simple assertion to test if the method concatenates the strings correctly.
Add the following helloWorldCheck method to test Utils. You do this by interrupting the test thread after milliseconds.
If the thread is interrupted the test method throws a TimeoutException. Add the following code displayed in bold to set the timeout and to interrupt the thread if the method takes too long to execute. Add the following property displayed in bold to the Test annotation to specify that the test is expected to throw IllegalArgumentException. In JUnit 4 you simply add the Ignore annotation to disable the test. Add the Ignore annotation displayed in bold above Test to disable the test.
Now that you have written the tests you can run the test and see the test output in the JUnit Test Results window. You can run JUnit tests on the entire application or on individual files and see the results in the IDE. When you run UtilsJUnit4Test. If the class passes all the tests you will see something similar to the following image in the JUnit Test Results window.
The println that you added to each of the test methods printed out the name of the test to Test Results window and the Output window.
You can see that in UtilsJUnit4Test the test class initializer method annotated with BeforeClass was run before any of the other methods and it was run only once. The test class finalizer method annotated with AfterClass was run last, after all the other methods in the class.
The test initializer method annotated with Before was run before each test method. The controls in the left side of the Test Results window enable you to easily run the test again.
You can use the filter to toggle between displaying all test results or only the failed tests. The arrows enable you to skip to the next failure or the previous failure.
The next step after creating your unit test classes is to create test suites. See Creating JUnit 4 Test Suites to see how to run specified tests as a group so you do not have to run each test individually.
When creating tests for a project you will generally end up with many test classes. While you can run test classes individually or run all the tests in a project, in many cases you will want to run a subset of the tests or run tests in a specific order.
You can do this by creating one or more test suites. For example, you can create test suites that test specific aspects of your code or specific conditions.
A test suite is basically a class with a method that invokes the specified test cases, such as specific test classes, test methods in test classes and other test suites.
A test suite can be included as part of a test class but best practices recommends creating individual test suite classes. When you use the IDE to generate a test suite, by default the IDE generates code to invoke all the test classes in the same package as the test suite.
After the test suite is created you can modify the class to specify the tests you want to run as part of that suite. If you selected JUnit 3 as the version for your tests, the IDE can generate JUnit 3 test suites based on the test classes in the test package. In JUnit 3 you specify the test classes to include in the test suite by creating an instance of TestSuite and using the addTest method for each test.
Select the sample package to create the test suite in the sample folder in the test packages folder. When you click Finish, the IDE creates the test suite class in the sample package and opens the class in the editor. The test suite will contain the following code. Modify the suite method to add the test classes that will be run as part of the suite. In JUnit 4 test suites you specify the test classes to include as values of the Suite annotation. The test suite contains code similar to the following.
When you run the test suite the IDE runs the tests included in the suite in the order they are listed. The results are displayed in the JUnit Test Results window. In this image click the image to see a larger image you can see the test results for a JUnit 3 test suite. The test suite ran the UtilsJUnit3Test and VectorsJUnit3Test test classes as a single test and displayed the test results in the left pane as the results of a single test. The output in the right pane is the same as when you run the test individually.
In this image click the image to see a larger image you can see the test results for a JUnit 4 test suite. The test suite ran the UtilsJUnit4Test and VectorsJUnit4Test test classes as a single test and displayed the test results in the left pane as the results of a single test. In this image click the image to see a larger image you can see the test results for a mixed test suite. This test suite includes the JUnit 4 test suite and one of the JUnit 3 test classes.
The test suite ran the UtilsJUnit3Test. The output in the right pane is the same as running the test individually. The IDE supports JUnit 3 and JUnit 4, and this document demonstrated some of the changes introduced in JUnit 4 that are designed to make creating and running tests simpler.
As demonstrated in this tutorial, one of the main improvements in JUnit 4 is support for annotations. In JUnit 4 you can now use annotations to do the following:. Identify setUp and tearDown methods with Before and After annotations. Identify setUp and tearDown methods that apply to the entire test class. Methods annotated with BeforeClass are run only once, before any test methods in the class are run. Methods annotated with AfterClass are also run only once, after all the test methods have finished.
For more information about using JUnit and other changes introduced in JUnit 4, see the following resources:. Testing code often helps ensure that small changes made in the code do not break the application. Automated testing tools like JUnit streamline the process of testing and frequent testing can help catch coding errors early. Apache NetBeans. Latest release. For more on using JUnit, see www. Type JUnit-Sample for the project and set the project location.
Deselect the Use Dedicated Folder option, if selected. Creating the Java Classes In this exercise you copy the files Utils. Type sample as the package name. Click Finish. You can close the JUnitSampleSol project because you will not need it again. The JUnitSampleSol project contains all the tests described in this document.
Creating a Test Class for Vectors. Otherwise, you will be prompted to select the necessary test from a popup or create a new test. Create tests The simplest way of creating a new test class in IntelliJ IDEA is by using a dedicated intention action that you can invoke from your source code. Create Test dialog controls. Item Description Testing library Select the testing framework that you are going to use.
Fix This button is available when a library for the selected testing framework is missing. Class name Enter the name for the test class or accept the default name. Superclass For JUnit3, the superclass junit. Destination package Specify the name of the package where the generated test class will be stored. Show inherited methods Select this option to show all methods, including the inherited ones.
Generate test methods for Select the methods for which you want to generate test methods.
0コメント