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.
Posted Date:- 2021-08-30 06:55:59
How to write a simple Junit test class?
What do you understand by JWebUnit? And what are its advantages?
Why does JUnit only report the first failure in a single test?
Is it possible to pass command-line arguments to a test execution?
Can we change return type of JUnit test method from void to some other type?
When you should run JUnit test, Is there any particular time interval between each run?
What’s the use of @Rule annotation?
What is Parameterized test in JUnit and what all annotations used for this?
What is unit testing method-naming convention that you follow?
What is mocking and stubbing? Did you use any mocking framework?
How is code cyclomatic complexity related to unit tests?
What are text fixtures in unit testing?
Where does the test garbage in JUnit go?
Why should you refrain from using System.out.printIn() for debugging?
What are text fixtures in unit testing?
Define (i) Fixtures (ii) Test Suit (iii) Test Runner (iv) JUnit Classes
What happens in JUnit when the return type is “string�
How do you test the ‘protected’ and ‘private’ methods?
Do you have to write a test case for every logic?
Differentiate between manual and automated testing.
What does @Rule annotation do?
Can you test a private method using JUnit?
How do you test protected Java methods using JUnit?
What happens when your JUnit test method throws an exception?
What does @RunWith annotation do?
What are some best practices you follow while writing code to make it more testable?
Give a thorough analysis of the Joint Test API Methods?
What do you mean by parameterized tests in JUnit?
Shed your views on the @ignore annotation. Even describe its usefulness?
Shed light on the reason that why JUnit only reports the first failure in a single attempt?
How is code cyclomatic complexity related to unit tests?
What are the top advantages of writing unit tests?
How do you ignore certain test methods in the JUnit Test class?
What is a test fixture with respect to JUnit?
Give a brief account of the Test Suite with respect to JUnit?
What is difference between setUp() and tearDown() method?
How does JUnit achieve the isolation of tests?
Define installation with respect to JUnit?
What are the critical fixtures of JUnit?
What is mocking and stubbing? Did you use any mocking framework?
How is code cyclomatic complexity related to unit tests?
Explain how you can write a simple JUnit test case?
Explain what is Unit Test Case?
What are the important JUnit annotations?
What is the difference between @Before and @BeforeClass annotation in JUnit?
What do @Before and @After annotation does in JUnit 4?