Você está na página 1de 4

SVN

Repository is created by them only after completing our


work we have to put our code into repository and make
commit..
Second person open the system and update
The project and do some modifications then he also do
commit if you want that modified project then you also
open ur project with update

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 do you test a "private" method?


A: When a method is declared as "private", it can only be accessed within the same class. So there is no way to
test a "private" method of a target class from any test class. Hence you need to perform unit testing manually. Or
you have to change your method from "private" to "protected".

Q: What happens if a JUnit test method is declared to return "String"?


A: If a JUnit test method is declared to return "String", the compilation will pass ok. But the execution will fail. This
is because JUnit requires that all test methods must be declared to return "void".

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: What are Parameterized tests in JUnit?


A: Parameterized tests allow developer to run the same test over and over again using different values.

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

Annotation & Description


@Test

The @Test annotation identifies a method as a test method..


@Before

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

perform clean-up activities, for example, to disconnect from a database. Methods


annotated with this annotation need to be defined as static to work with JUnit.
@Ignore
The Ignore annotation is used to ignore the test and that test will not be executed.

if the execution time of this test is too long to be included

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)

Checks that the boolean condition is true.


assertFalse([message], boolean condition)
Checks that the boolean condition is false.
assertEquals([String message], expected, actual)
Tests that two values are the same. Note: for arrays the reference is checked not the content of
the arrays.
assertEquals([String message], expected, actual, tolerance)
Test that float or double values match. The tolerance is the number of decimals which must be
the same.
assertNull([message], object
Checks that the object is null.
assertNotNull([message], object)
Checks that the object is not null.

assertSame([String], expected, actual)


Checks that both variables refer to the same object
assertNotSame([String], expected, actual)
Checks that both variables refer to different objects.

Create a JUnit test suite


If you have several test classes, you can combine them into a test suite. Running a test suite
will execute all test classes in that suite in the specified order.
The following example code shows a test suite which defines that two test classes should be
executed. If you want to add another test class you can add it to @Suite.SuiteClasses statement.

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" })

public class CompanyServiceTest {

...

@ContextConfiguration
Set the spring ApplicationContext for your test classes using
@ContextConfiguration annotation.
1

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = { "/spring-servlet-test.xml" })

public class CompanyServiceTest {

...

Você também pode gostar