0% found this document useful (0 votes)
124 views7 pages

Junit

1) JUnit is a unit testing framework for Java that allows testing code at the method level. It is commonly used by developers for test-driven development by writing tests first before code. 2) The main difference between JUnit 3 and JUnit 4 is that JUnit 4 uses annotations like @Test instead of requiring test method names to start with "test". 3) The @Before and @After annotations in JUnit 4 are used for setup and cleanup jobs, replacing the setUp() and tearDown() methods of JUnit 3. @Before runs before each test and @After runs after.

Uploaded by

Neha Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views7 pages

Junit

1) JUnit is a unit testing framework for Java that allows testing code at the method level. It is commonly used by developers for test-driven development by writing tests first before code. 2) The main difference between JUnit 3 and JUnit 4 is that JUnit 4 uses annotations like @Test instead of requiring test method names to start with "test". 3) The @Before and @After annotations in JUnit 4 are used for setup and cleanup jobs, replacing the setUp() and tearDown() methods of JUnit 3. @Before runs before each test and @After runs after.

Uploaded by

Neha Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1) What is Testing?

Testing is the process of checking the functionality of the application whether it fulfills the requirement or
not.

2) What is JUnit?
JUnit is the testing framework, it is used for unit testing of Java code.
1. JUnit = Java + Unit Testing  

3) What is unit testing?


The process of testing individual functionality (known as a unit) of the application is called unit
testing.3.9Stay

4) What is the difference between manual testing and automated testing?


Manual testing is performed by Human, so it is time-consuming and costly. Automated testing is performed
by testing tools or programs, so it is fast and less costly.

5) Give some disadvantages of manual testing.


Following are some disadvantages of manual testing:
o The testing is very time consuming and is very tiring.
o The testing demands a very big investment in the human resources.
o The testing is less reliable
o The testing cannot be programmed.

6) List out some advantages of automated testing.


Some of the advantages of automated testing are:
o It is very fast.
o Investment is very less.
o Testing is more reliable.
o The testing can be programmed.

7) Is it necessary to write the test case for every logic?


No, we should write the test case only for that logic that can be reasonably broken.

8) What are the useful JUnit extensions?


o JWebUnit
o XMLUnit
o Cactus
o MockObject

9) What are the features of JUnit?


o Opensource
o Annotation support for test cases
o Assertion support for checking the expected result
o Test runner support to execute the test case
10) How is the testing of the 'protected' method done?
To test the protected method, the test class is declared in the same package as the target class.

11) How is the testing of 'private' method done?


There is no direct way for testing of the private method; hence manual testing is to be performed, or the
method is changed to "protected" method.

12) If the JUnit method's return type is 'string', what will happen?
JUnit test methods are designed to return 'void'. So the execution will fail.

13) Is the use of 'main' method possible for unit testing?


Yes

14) Is it necessary to write the test class to test every class?


No

15) What does XMLUnit provide?


Junit extension class, XMLTestCase and set of supporting classes is provided by the XMLUnit.

16) List the core components of Cactus.


o Cactus Framework
o Cactus Integration Module

17) What are the methods in fixtures?


o setup
o tearDown

18) What is the Unit Test Case?


A Unit Test Case is the combination of input data and expected output result. It is defined to test the
functionality of a unit.

19) What is the use of @Test annotation?


The @Test annotation is used to mark the method as the test method.

20) What is the test suit?


The test suit allows us to group multiple test cases so that it can be run together. TestSuit is the container
class under junit.framework.TestSuite package.

21) What does test runner?


The test runner is used to execute the test cases.

22) What are the important JUnit annotations?


The test runner is used to execute the test cases.
o @Test
o @BeforeClass
o @Before
o @After
o @AfterClass

23) What does Assert class?


Assert class provides methods to test the test cases.

1) Explain what is JUnit?


JUnit is a testing framework for unit testing.  It uses Java as a programming platform, and it is an Open
Source Software managed by the JUnit.org community.
2) Explain what is Unit Test Case?
Unit Test Case is a part of the code that ensures that the another part of code (method) behaves as
expected. For each requirement, there must be at least two test cases one negative test and one positive
test.
3) Explain how you can write a simple JUnit test case?
 Determine a subclass of TestCase
 To initialize object(s) under test, override the setup() method
 To release object(s) under test override the teardown() method
Determine one or more public test XYZ() methods that exercise the objects under test and assert expected
results.
4) Mention what are parameterized tests?
Parameterized tests enable developer to perform the same test over and again using different values.
3.1M
5) Mention what is the difference between JUnit and TestNG?
                                JUnit                                    TestNG
 In JUnit the naming convention for
  In TestNG it is easier to understand
annotation is a bit complicated for, e.g.,
annotations like “BeforMethod”,
“Before”, “After” and “Expected”
“AfterMethod” and “ExpectedException”
 In JUnit, for a method declaration you have
 In TestNG, there is no restriction like you have
to follow a specific style like using
to declare methods in a specific format
“@BeforeClass” and “@AfterClass”.
 In TestNG method name constraint is not
 In JUnit method name constraint is present
present, and you can determine any test
 JUnit framework does not have
method names
“Parameterized Test” or “Dependency Test”
 TestNG use “dependOnMethods” to
feature
implement the dependency testing
 In JUnit grouping of test cases are not
 In TestNG, grouping of test cases is available
available
 In TestNG Parallel execution of Selenium test
 JUnit does not support parallel execution
cases are possible
on Selenium test cases
 It can rerun the failed tests
 It cannot re-run the failed cases

6) Mention different methods of exception handling in JUnit?


There are different methods of exception handling in JUnit
 Try catch idiom
 With JUnit rule
 With @Test annotation
 With catch exception library
 With customs annotation
7) Explain what is ignore test in JUnit?
When your code is not ready, and it would fail if executed then you can use @Ignore annotation.
 It will not execute a test method annotated with @Ignore
 It will not execute any of the test methods of test class if it is annotated with @Ignore
8) List out some useful JUnit extensions?
JUnit extensions include
 Cactus
 JWebUnit
 XMLUnit
 MockObject
9) Explain who should use JUnit – a developer or tester? Why you use JUnit to test your code?
JUnit is more often used by developers to implement unit tests in JAVA.  It is designed for unit testing that
is more a coding process and not a testing process. However, many testers and QA engineers use JUnit for
unit testing.
JUnit is used because
 It test early and does automate testing
 JUnit tests can be compiled with the build so that at unit level, regression testing can be done
 It allows test code re-usage
 JUnit tests behave as a document for the unit tests when there is a transfer
10) Explain what is JUnitCore Class?
JUnitCore class is an inbuilt class in JUnit package; it is based on Façade design pattern, this class is used to
run only definite test classes only.
11) Explain how you can run JUnit from the command window?
To run JUnit from the command window, you have to follow the steps
 Set the CLASSPATH
 Invoke the runner:
Java org.junit.runner.JUnitCore

1. What is JUnit?
JUnit is a unit testing framework for the Java application. It allows you to unit test your code at the method
level. You can also use JUnit for test-driven development e.g. first writing test and then writing actual code.
Most of the Java IDE like Eclipse, Netbeans and IntelliJ Idea provides out-of-box JUnit integration for TDD.
Even build tools like Maven, Ant, and Gradle can run the JUnit test at compile and built time.

2. What is the difference between JUnit 3.X and JUnit 4.x?


The main difference between JUnit 3 and JUnit 4 is annotation. Earlier you need to start your method name
as testXXX() but from JUnit 4.0 onwards you can use @Test annotation, which removes this dependency to
start your Junit test methods as a test.

3. What do @Before and @After annotation does in JUnit 4?


@Before and @After annotation is used for setup and cleanup jobs. They are actually a replacement of
the setUp() and tearDown() method of JUnit 3.X. Remember, a method annotated with @Before will be called
before calling a test method and @After will be called after executing the test method. 

So if you have 5 test methods in one JUnit test class, they will be called five times. You will be glad to know
that JUnit 5 has renamed @Before to @After to @BeforeEach and @AfterEach to make it more readable. 

4. What is the difference between @Before and @BeforeClass annotation in JUnit?


@Before is called per test method while @BeforeClass is called per-test-class level. Also, static methods are
annotated with @BeforeClass and you can use them to initialize shared resources e.g. which should be
shared by every instance of JUnit class like Database connection.

5. How does JUnit achieve the isolation of tests?


JUnit creates a separate instance of the test class for calling test methods. For example, if your test class
contains 5 tets then JUnit will create 5 instances of the test class for executing those test methods. That's
why constructor is called before executing @Test methods. In JUnit 3.X, It used to first create instances and
then call the methods but in JUnit 4.1, it first creates an instance and then calls them the method and then
goes on to create the second method. This way, it keeps one test isolated from the other.

6. What is difference between setUp() and tearDown() method?


They are methods from JUnit 3.X which is replaced by @Before and @After annotated method. They are
used for setting up resources before the test method and for doing clean-up after-test methods.

7. How do you check if a test method throws Exception in JUnit?


In JUnit 4, you can use @Test annotation with an expected parameter to specify the exception thrown by a
test method. If the test method throws that Exception then JUnit will mark your test passed, otherwise, it will
mark them as failed. 

For example, if you want to verify your method should throw IllegalArgumentException if the given
parameter is bad then you can annotate your test method as @Test(expected =
IllegalArgumentException.class). In earlier versions e.g. on JUnit 3.X, you could have used catch block to
verify if the method under test throws a particular exception or not.

8. How do you ignore certain test methods in the JUnit Test class?
Well, from JUnit 4 onwards you can use @Ignore annotation to instruct JUnit runtime to not execute any
particular test or disable any test as shown in this JUnit ignore example. You can also use this method for
annotating unimplemented test methods. Btw, you should use @Ignore only temporarily and remove it as
soon as possible by either implementing the test methods or removing it if not required.

9. Can you test a Java method for timeout using JUnit?


Yes, 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. 

10. What are some best practices you follow while writing code to make it more testable?
Here are some of the best practices you can follow to make your code more testable :

1) Use interface instead of concrete class, this allows the tester to replace actual class with Mock or Stub for
testing purposes.

2) Use Dependency injection, it makes it easy to test individual parts and supply dependency from test
configuration. You can create an actual object which is dependent on the Mock and stub for testing. This
also allows you to test a class even if its dependency is not yet coded.

3) Avoid static methods, they are difficult to test as they cannot be called polymorhpically.

11. What does @RunWith annotation do?


The @RunWith annotation allows you to run your JUnit test with another, custom JUnit Runner. 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. 

Some of the popular third-party implementations of JUnit runners


include SpringJUnit4ClassRunner and MockitoJUnitRunner, which you can use with @RunWith annotation to
test Spring beans and Mockito library.

12. how do you test a method which doesn't return a value?


Well, you can test a method even if it doesn't return a value by checking for its side effects, for
example, add() method of ArrayList doesn't return anything but it has a side effect of the increasing size of
Collection by 1, so you can check whether add() is successful or not by checking the size() of List before
and after calling this method as shown below:

@Test
public void testListAdd() {
List<String> mylist = new ArrayList<>();
assertEquals(0, mylist.size());
collection.add("Java");
assertEquals(1, mylist.size());
collection.add("JUnit");
assertEquals(2, mylist.size());
}

If a method doesn't return any value and doesn't have any side effects which means it's not doing anything
and doesn't require testing.

10. What is the difference between Stub and Mock in Unit testing?
Though both Stub and Mock are tools for unit testing and used in place of actual class to facilitate unit
testing there is some subtle difference between them. Mocks are generally more powerful becuase they
can record method calls and there are also good frameworks like Mockito and PowerMock available,
which can create Mock for you. 
Stubs are easier to use but require more coding than Mocks becuase you need to create your own Stub
implementation. Many Java developers prefer mocks over stubs becuase of this reason only.

14. What happens when your JUnit test method throws an exception?
When a JUnit method throws an uncaught exception, which is also not declared as expected using
the @Test(expected) parameter then the JUnit test will fail, but if it's declared and expected then the JUnit
test will pass. If the method under test throws an exception that is caught by you inside the JUnit test
method then nothing will happen and the test will be passed

15. How do you test protected Java methods using JUnit?


Since protected methods are accessible to everyone inside the same package they are declared, you can
put your JUnit test class also on the same package to test them. But, don't mix your test code with real
code, instead, put them on a different source tree. You can use a Maven-like structure to organize your test
and source classes in different folders.

16. Can you test a private method using JUnit?


Well, since private methods are not accessible outside the class they are declared, it's not possible to test
them using JUnit directly, but you can use Reflection to make them accessible by
calling setAccessible(true) before testing. Another way to test private methods is via public methods, which
uses them. In general, testing the private method is also an indication that those methods should be moved
into another class to promote reusability.

17. What are some Unit testing libraries you know other than JUnit for Java programs?
Java is very lucky to have many great unit testing libraries including JUnit. Here are a couple of the most
popular ones:

 Mockito which is a Mocking framework can be used with JUnit


 PowerMock, similar to Mockito but also provide mocking of static methods.
 TestNG is similar to JUnit
 Spock groovy-based unit testing framework for the Java application. Thanks to JUnit Runner, you
can use the Spock testing framework with Java IDEs as well.

At a bare minimum, you should JUnit and Mockito for writing unit tests in Java. 

18. Does JUnit support Parameterized tests, where tests can be executed multiple times with
different parameter values?
Yes, JUnit supports parameterized tests by using a custom Runner called Parameterized
and @Parameters annotation. You use this annotation to annotate the method which provides parametric
values and run the Test class with @RunWith(Parameterized. class) runner. 

19. What does @Rule annotation do?


It allows you to create JUnit rules, which can be used to add or redefine the behavior of each test method in
a test class. You can write your own rules using @Rule annotation.

20. What are text fixtures in unit testing?


A test fixture in unit testing is used to provide a well-known and fixed environment in which the test can be
run. A text fixture could be set up or mock objects or loading specific data from the database for testing.
JUnit provides @Before and @BeforeClass annotation for creating text fixtures
and @After and @Afterclass for doing cleanup.

21. What is the difference between JUnit 4 and JUnit 5?


The JUnit 5 is the latest version of JUnit also known as Jupiter.  The minimum JDK for JUnit 4 was JDK 5,
while JUnit 5 requires at least JDK 8. Also, the @Before, @BeforeClass, @After,
and @AfterClass annotations are now more readable than the @BeforeEach, @BeforeAll, @AfterEach,
and @AfterAll annotations.

You might also like