JUnit = Java + Unit Testing
@Test
: This annotation over a public method of void return type can be run as a test case. This is a replacement of the org.junit.TestCase annotation.@Before
: This is used when we want to execute the preconditions or any initialisation based statements before running every test case.@BeforeClass
: This is used when we want to execute statements before all test cases. The statements may include test connections, common setup initialisation etc.@After
: This is used when we want to execute statements after each test case. The statements can be resetting the variables, deleting extra memory used etc.@AfterClass
: This is used when certain statements are required to be executed after all the test cases of the class are run. Releasing resource connections post-execution of test cases is one such example.@Ignores
: This is used when some statements are required to be ignored during the execution of test cases.@Test(timeout=x)
: This is used when some timeout during the execution of test cases is to be set. The value of x is an integer that represents the time within which the tests have to be completed.@Test(expected=NullPointerException.class)
: This is used when some exception thrown by the target method needs to be asserted.fixture
has the following 2 methods :setUp()
This runs before every test case is run.tearDown()
This method is run after every test is run.JUnit
provides a timeout parameter to @Test annotation
which can be used to instruct JUnit to pass or fail a test method based upon execution time. For example, a JUnit
test method annotated with @Test(timeout= 50)
will be failed it doesn't complete in 50 milliseconds (timeout is specified in millisecond)
. This is the standard way to verify the SLA
of a Java method. @RunWith
annotation allows you to run your JUnit test with another, custom JUnit Runne
r. For example, you can do some parameterized testing in JUnit by using @RunWith(Parameterized. class)
, this Runner is required for implementing parameterized tests in JUnit. JUnit
runners include SpringJUnit4ClassRunner
and MockitoJUnitRunner
, which you can use with @RunWith annotation
to test Spring beans and Mockito
library.JUnit
, both Run With and Suite comments are being used to avail maximum benefits. @RunWith
annotation lets us run the JUnit tests with other custom JUnit Runners like SpringJUnit4ClassRunner
, MockitoJUnitRunner
etc. We can also do parameterized testing in JUnit by making use of @RunWith(Parameterized.class)
. JUnit 4
. Methods annotated with @Before
will be called and run before the execution of each test case. Methods annotated with @Afte
r will be called and executed after the execution of each test case. If we have 5 test cases in a JUnit class, then the methods annotated with these two would be run 5 times. In JUnit 5
, the annotations @Before
and @After
are renamed to @BeforeEach
and @AfterEach
for making it more readable. ystem.out.println()
for debugging, then we would have to do a manual scan of the outputs every time program is executed to ensure that the output printed is the expected output. Also, in the longer run, writing JUnit tests will be easier and will take lesser time. Writing test cases will be like an initial investment and then, later on, we can later automatically test them on the classes. @before
class@after
class@before
performs@before
so that the application development process is smooth and is hassle freeJUnit.zip.
JUnit.zip
file to a separate directory also referred to as the %JUNIT_HOME%
CLASSPATH=%CLASSPATH%;%JUNIT_HOME%junit.jar
org.junit.runner.JUnitCore org.junit.tests.AllTests
to run various kind of testscyclomatic complexity
is determined based on number of decision points within the code and hence execution paths, higher cyclomatic complexity makes it difficult to attain achieve test/code
coverage. @Before annotation
:@Before
public void myMethod()
@BeforeClass annotation
:@BeforeClass
public static void myMethod()
JDK
is installed and the “java
” command program is accessible through the PATH setting. Type “java -version
” at the command prompt, you should see the JVM reports you back the version string.java org.junit.runner.JUnitCore
@Test
annotation.XPath
also called XML Path
is a language to query XML documents
. It is an important strategy to locate elements in selenium. It consists of a path expression along with some conditions. Here, you can easily write XPath script/query
to locate any element in the webpage. It is designed to allow the navigation of XML
documents, with the purpose of selecting individual elements, attributes, or some other part of an XML document
for specific processing. It also produces reliable locators. XPath
is that, if there are any changes made in the path of the element then that XPath
gets failed. For example : /html/body/div[1]/section/div[1]/div
XPath
, the path starts from the middle of the HTML DOM structure. It begins with the double forward slash (//
), which means it can search the element anywhere at the webpage. For example : //input[@id=‘ap_email’]
request
” to that website’s server, and the server sends back a response in the form of the website you want to access. To load test a website, quality assurance engineers and automation engineers just need to multiply the number of responses sent to simulate different traffic loads. The web server’s response to the influx of virtual users can then be measured. This is used to determine performance issues and server capacity. expect-run-verify
development patterns by removing external specifications and dependencies. Some of the advantages of Mockito are :doReturn
are used for setting up stubs and are provided by the Mockito framework. They are generally used along with when clause as --> when-thenReturn
and doReturn-when
. when-thenReturn
is used in most cases due to better readability. thenReturn
and doReturn
:doReturn
takes Object parameter, unlike thenReturn
. Hence there is no type check in doReturn
at compile time. In the case of thenReturn
, whenever the type mismatches during runtime, the WrongTypeOfReturnValue
exception is raised. This is why when-thenReturn
is a better option whenever we know the type.doReturn
does not have type safety, there are no side effects when it is being used. Whenever thenReturn
is used, if the type mismatch occurs, an exception is thrown which might result in the abortion of test cases.@Mock
annotation is used for creating mocks whereas @InjectMocks
is used for creating class objects. Whenever the actual method body has to be executed of a given class, then we can use @InjectMocks
. PowerMock
that supports static methods. JWebUnit
provides a high-level Java API for navigating a web application combined with a set of assertions to verify the application's correctness. This includes navigation via links, form entry and submission, validation of table contents, and other typical business web application features.HtmlUnit
. And if you want to switch from HtmlUnit
to other plugins such as Selenium
(available soon), there is no need to rewrite your tests.