Execute Log4j Logger statements in JUNIT to increase the code coverage
Let us have a Student class
class Student{
private static final Logger LOGGER = Logger
.getLogger(Student.class);
public void showStudent(){
LOGGER.debug("Enter in showStudent method");
/*
Method Body
*/
LOGGER.debug("Exit from showStudent method");
}
}
Let us have another Test class StudentTest, which test the functionality of Student class
class StudentTest extends TestCase {
private static final Logger LOGGER = Logger
.getLogger(Student.class);
Student student;
@Before
public void setUp() throws Exception {
student = new Student();
LOGGER.setLevel(Level.DEBUG);
}
@Test
public void testShowStudent(){
student.showStudent();
}
}
In this class we create a Logger object, and pass Student.class as an argument.
private static final Logger LOGGER = Logger
.getLogger(Student.class);
and set loglevel in setUp() method
LOGGER.setLevel(Level.DEBUG);
Now when we execute student.showStudent() method in our test case then its logger statements also executes because we create an instance of Logger object and set its level to debug (i.e. Level.DEBUG)
Let us have a Student class
class Student{
private static final Logger LOGGER = Logger
.getLogger(Student.class);
public void showStudent(){
LOGGER.debug("Enter in showStudent method");
/*
Method Body
*/
LOGGER.debug("Exit from showStudent method");
}
}
Let us have another Test class StudentTest, which test the functionality of Student class
class StudentTest extends TestCase {
private static final Logger LOGGER = Logger
.getLogger(Student.class);
Student student;
@Before
public void setUp() throws Exception {
student = new Student();
LOGGER.setLevel(Level.DEBUG);
}
@Test
public void testShowStudent(){
student.showStudent();
}
}
In this class we create a Logger object, and pass Student.class as an argument.
private static final Logger LOGGER = Logger
.getLogger(Student.class);
and set loglevel in setUp() method
LOGGER.setLevel(Level.DEBUG);
Now when we execute student.showStudent() method in our test case then its logger statements also executes because we create an instance of Logger object and set its level to debug (i.e. Level.DEBUG)
How code coverage works in Eclipse.
ReplyDeleteHi Amit,
DeleteIts a plugin of eclipse.
Download it and then it works for you.
You can follow this link
http://codecover.org/documentation/references/eclManual.html
Good one. Can you please help me in finding the correct AT commands for unlocking the LTE data card
ReplyDeletePlease give some light how it helps to increase code coverage.
ReplyDeleteHi Arun,
DeleteAs i mentioned above, Initialize Logger object in your test calss, like that
private static final Logger LOGGER = Logger
.getLogger(Student.class);
and then in setUp() method set its level by setLevel(..) like that
LOGGER.setLevel(Level.DEBUG);
now when you test any method then its LOGGER.debug statements also executes, which increase your code coverage.
Logger has multiple flows in multi-threading, how to encounter these flows.
ReplyDelete