Escolar Documentos
Profissional Documentos
Cultura Documentos
JUNIT
Q: How do you test a "protected" method?
A: When a method is declared as "protected", it can only be accessed within the same package where the class
is defined. Hence to test a "protected" method of a target class, define your test class in the same package as the
target class.
Q: How can you use JUnit to test that the code throws desired exception?
A: JUnit provides a option of tracing the Exception handling of code. You can test if a code throws desired
exception or not. The expected parameter is used along with @Test annotation as follows: @Test(expected)
Q: Do you need to write a test class for every class that needs to be tested?
A: No. We need not write an independent test class for every class that needs to be tested. If there is a small
group of tests sharing a common test fixture, you may move those tests to a new test class.
Annotation
Annotations are like meta-tags that you can add to you code and apply them to methods or in class. These
annotation in JUnit gives us information about test methods , which methods are going to run before & after test
methods, which methods run before & after all the methods, which methods or class will be ignore during
execution.
S.N
.
1
This method is executed before each test. It is used to prepare the test
environment
@After
This method is executed after each test. It is used to cleanup the test environment
(e.g., delete temporary data, restore defaults). It can also save memory by cleaning
up expensive memory structures.runs. Annotating a public void method with @After causes
that method to be run after the Test method.
@BeforeClass
This method is executed once, before the start of all tests. It is used to perform
time intensive activities, for example, to connect to a database. Methods marked
with this annotation need to be defined asstatic to work with JUnit.
@AfterClass
. This method is executed once, after all tests have been finished. It is used to
fail(String)
Let the method fail. Might be used to check that a certain part of the code is not reached or to
have a failing test before the test code is implemented. The String parameter is optional.
assertTrue([message], boolean condition)
package com.vogella.junit.first;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ MyClassTest.class, MySecondClassTest.class })
public class AllTests {
@RunWith
When a class is annotated with @RunWith or extends a class annotated
with @RunWith, JUnit will invoke the class it references to run the tests in
that class instead of the runner built into JUnit. Let us configure jUnit to use
Spring jUnit Class runner.
1
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring-servlet-test.xml" })
...
@ContextConfiguration
Set the spring ApplicationContext for your test classes using
@ContextConfiguration annotation.
1
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring-servlet-test.xml" })
...