A unit test is a piece of code written by a developer that executes a specific functionality in the code under test. Unit tests ensure that code is working as intended and validate that this is still the case after code changes.
JUnit 4.x is a test framework
which uses annotations to identify
methods that are tests.
JUnit
assumes that all test methods
can
be executed
in an
arbitrary order.
Therefore tests
should not depend on
other tests.
To write a test with JUnit
You can use Eclipse or the class "org.junit.runner.JUnitCore" to run the test.
To write a test with JUnit
-
Annotate a method with @org.junit.Test
-
Use a method provided by JUnit to check the expected result
of the code execution versus the actual result
You can use Eclipse or the class "org.junit.runner.JUnitCore" to run the test.
If you use Eclipse you can use the integrated JUnit in Eclipse
for your testing.
If you want to control the used JUnit library explicitly, download JUnit4.x.jar from the JUnit website at http://www.junit.org/ . The download contains the "junit-4.*.jar" which is the JUnit library. Add this library to your Java project and add it to the classpath.
If you want to control the used JUnit library explicitly, download JUnit4.x.jar from the JUnit website at http://www.junit.org/ . The download contains the "junit-4.*.jar" which is the JUnit library. Add this library to your Java project and add it to the classpath.
Create a new project
Create a new source folder
Press "Add folder" then press "Create new folder". Create the folder "test".
Alternatively you can add a new source folder by right-clicking on a project and selecting → .
de.vogella.junit.first
. We want to create
the unit tests in a separate folder. The creation
of a separate folder for tests is not
mandatory. But it is a good
practice to keep the code separated
from the regular code.
You might
even create a separate project for the test
classes, but we skip this
step to make this example simpler.
Create a new source folder
test
via
right-clicking on your
project,
select "Properties"
and choose the
"Java
Build Path". Select the "Source"
tab.
Press "Add folder" then press "Create new folder". Create the folder "test".
Alternatively you can add a new source folder by right-clicking on a project and selecting → .
In the "src" folder, create the
de.vogella.junit.first
package
and the following
class.
package de.vogella.junit.first; public class MyClass { public int multiply(int x, int y) { return x / y; } }
Right click on your new class in the Package Explorer and select
→ . Select "New JUnit 4
test" and set the source folder to "test",
so
that your test class gets created in this folder.
Press "Next" and select the methods which you want to test.
If the JUnit library in not part of your classpath, Eclipse will prompt you to do so.
Create a test with the following code.
Press "Next" and select the methods which you want to test.
If the JUnit library in not part of your classpath, Eclipse will prompt you to do so.
Create a test with the following code.
package de.vogella.junit.first; import org.junit.Test; import static org.junit.Assert.assertEquals; public class MyClassTest { @Test public void testMultiply() { MyClass tester = new MyClass(); assertEquals("Result", 50, tester.multiply(10, 5)); } }
Right click on your new test class and select
→ .
The result of the tests will be displayed in the JUnit
The test should be failing (indicated via a red bar).
This is because our multiplier class is currently not working correctly (it does a division instead of multiplication). Fix the bug and re-run test to get a green bar.
If you have several tests you can combine them into a test suite. Running a test suite will execute all tests in that suite.
To create a test suite, → → → → → .
Select "Next" and select the methods for which you want to create a test.
Change the code to the following to make your test suite run your test. If you develop another test later you can add it to
The result of the tests will be displayed in the JUnit
View
.
The test should be failing (indicated via a red bar).
This is because our multiplier class is currently not working correctly (it does a division instead of multiplication). Fix the bug and re-run test to get a green bar.
If you have several tests you can combine them into a test suite. Running a test suite will execute all tests in that suite.
To create a test suite, → → → → → .
Select "Next" and select the methods for which you want to create a test.
Change the code to the following to make your test suite run your test. If you develop another test later you can add it to
@Suite.SuiteClasses
.
package mypackage; import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses( { MyClassTest.class }) public class AllTests { }
No comments:
Post a Comment