Junit automation testing framework index.
JUnit is a simple framework to write repeatable tests. It is a Java based framework and is an instance of the xUnit architecture for unit testing frameworks.
Check out the latest blogs from LambdaTest on this topic:
Are you comfortable pushing a buggy release to a staging environment?
Both JUnit and TestNG are popular unit testing frameworks that have been widely accepted by Java developers. JUnit was introduced as an open-source unit testing framework for Java way back in 1997. In fact, JUnit is one of the widely used test automation frameworks for test automation. TestNG is another Java-based test automation framework that is not only open-source but also offers awesome features that are best suited for large-scale web automation testing. TestNG was created for a range of testing categories, including (but not limited to) unit testing, functional testing, end-to-end testing, and integration testing.
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Selenium Python Tutorial and Selenium Cucumber .
Test Coverage and Code coverage are the most popular methodologies for measuring the effectiveness of the code. Though these terms are sometimes used interchangeably since their underlying principles are the same. But they are not as similar as you may think. Many times, I have noticed the testing team and development team being confused over the use of these two terminologies. Which is why I thought of coming up with an article to talk about the differences between code coverage and test coverage in detail.
During the process of test automation, you would come across a number of scenarios where a decision needs to be taken regarding “What if the test(s) result in a failure?” If the error (or issue) being encountered is a minor one, you might want the test execution to continue. In case of serious errors, it is better to abort the execution of the test case (or test suite). This can be achieved using ‘Assert and Verify in Selenium WebDriver Tutorial.
LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Junit is lincensed under the Other
Ask and answer questions on LambdaTest community. Visit now!
Initialising mock objects - Mockito
Run Junit-Tests from several projects conveniently fast in Eclipse
assertAll vs multiple assertions in JUnit5
Embedded MongoDB when running integration tests
Plugin 'org.springframework.boot:spring-boot-maven-plugin:' not found
How to know the version of JUnit in Eclipse
Injecting @Autowired private field during testing
junit: no tests found
How to test a Jersey REST web service?
How do I calculate someone's age in Java?
For the mocks initialization, using the runner or the MockitoAnnotations.initMocks
are strictly equivalent solutions. From the javadoc of the MockitoJUnitRunner :
JUnit 4.5 runner initializes mocks annotated with Mock, so that explicit usage of MockitoAnnotations.initMocks(Object) is not necessary. Mocks are initialized before each test method.
The first solution (with the MockitoAnnotations.initMocks
) could be used when you have already configured a specific runner (SpringJUnit4ClassRunner
for example) on your test case.
The second solution (with the MockitoJUnitRunner
) is the more classic and my favorite. The code is simpler. Using a runner provides the great advantage of automatic validation of framework usage (described by @David Wallace in this answer).
Both solutions allows to share the mocks (and spies) between the test methods. Coupled with the @InjectMocks
, they allow to write unit tests very quickly. The boilerplate mocking code is reduced, the tests are easier to read. For example:
@RunWith(MockitoJUnitRunner.class)
public class ArticleManagerTest {
@Mock private ArticleCalculator calculator;
@Mock(name = "database") private ArticleDatabase dbMock;
@Spy private UserProvider userProvider = new ConsumerUserProvider();
@InjectMocks private ArticleManager manager;
@Test public void shouldDoSomething() {
manager.initiateArticle();
verify(database).addListener(any(ArticleListener.class));
}
@Test public void shouldDoSomethingElse() {
manager.finishArticle();
verify(database).removeListener(any(ArticleListener.class));
}
}
Pros: The code is minimal
Cons: Black magic. IMO it is mainly due to the @InjectMocks annotation. With this annotation "you loose the pain of code" (see the great comments of @Brice)
The third solution is to create your mock on each test method. It allow as explained by @mlk in its answer to have "self contained test".
public class ArticleManagerTest {
@Test public void shouldDoSomething() {
// given
ArticleCalculator calculator = mock(ArticleCalculator.class);
ArticleDatabase database = mock(ArticleDatabase.class);
UserProvider userProvider = spy(new ConsumerUserProvider());
ArticleManager manager = new ArticleManager(calculator,
userProvider,
database);
// when
manager.initiateArticle();
// then
verify(database).addListener(any(ArticleListener.class));
}
@Test public void shouldDoSomethingElse() {
// given
ArticleCalculator calculator = mock(ArticleCalculator.class);
ArticleDatabase database = mock(ArticleDatabase.class);
UserProvider userProvider = spy(new ConsumerUserProvider());
ArticleManager manager = new ArticleManager(calculator,
userProvider,
database);
// when
manager.finishArticle();
// then
verify(database).removeListener(any(ArticleListener.class));
}
}
Pros: You clearly demonstrate how your api works (BDD...)
Cons: there is more boilerplate code. (The mocks creation)
My recommandation is a compromise. Use the @Mock
annotation with the @RunWith(MockitoJUnitRunner.class)
, but do not use the @InjectMocks
:
@RunWith(MockitoJUnitRunner.class)
public class ArticleManagerTest {
@Mock private ArticleCalculator calculator;
@Mock private ArticleDatabase database;
@Spy private UserProvider userProvider = new ConsumerUserProvider();
@Test public void shouldDoSomething() {
// given
ArticleManager manager = new ArticleManager(calculator,
userProvider,
database);
// when
manager.initiateArticle();
// then
verify(database).addListener(any(ArticleListener.class));
}
@Test public void shouldDoSomethingElse() {
// given
ArticleManager manager = new ArticleManager(calculator,
userProvider,
database);
// when
manager.finishArticle();
// then
verify(database).removeListener(any(ArticleListener.class));
}
}
Pros: You clearly demonstrate how your api works (How my ArticleManager
is instantiated). No boilerplate code.
Cons: The test is not self contained, less pain of code
Description:
Page text should be left-justified.
Description:
Verify that the API response contains the correct resource representation based on the specified locale (e.g. en-US, fr-FR).
Description:
Verify that the API correctly handles CORS preflight requests and returns the correct HTTP status code and error message.
Description:
Check all pages for broken links.
Junit can be downloaded from it’s GitHub repository - https://github.com/junit-team/junit5
Run Selenium, Cypress & Appium Tests Online on
3000+ Browsers.
Accelerate Automation test execution upto 70% faster with the
next-gen testing platform.
Scale your test execution with our cloud infrastructure paired
with your firewall.
World’s first end to end software testing agent.
Selenium is one of the most renowned open-source test automation frameworks. It allows test automation of web-apps across different browsers & operating systems.
TestNG is a popular open-source Java-based testing framework. It covers a broader range of test categories: unit, functional, end-to-end, integration, etc.
Serenity framework allows for cleaner and more maintainable automated acceptance and makes regression tests faster. This is an integration with JUnit.
Serenity framework allows for cleaner and more maintainable automated acceptance and makes regression tests faster. This is an integration with Cucumber.
Serenity framework allows for cleaner and more maintainable automated acceptance and makes regression tests faster. This is an integration with JBehave.
Tool to perform auditing and testing for inspecting infrastructure
Generate mocks from ActiveRecord models for unit tests that run fast because they don't need to load Rails or a database
Gherkin is a parser and compiler for the Gherkin language. Gherkin Ruby can be used either through its command line interface (CLI) or as a library.
factory_bot is a fixtures replacement with a straightforward definition syntax , support for multiple build strategies and support for multiple factories.
FlaUI is a .NET library which helps with automated UI testing of Windows applications (Win32, WinForms, WPF, Store Apps).
Perform automation testing with Junit on LambdaTest, the most powerful, fastest, and secure cloud-based platform to accelerate test execution speed.
Test Now