Blog Archives
1 2 3 4

01: JMockit interview Q&As & examples on @Injectable Vs @Mocked Vs @Capturing Vs @Tested

This extends 6 JUnit & JMockit interview questions & answers on Java Unit testing – Part 1. It is very essential to understand how these 3 annotations work in JMockit to write unit tests quickly & effectively.

Q1 What is the difference between @Injectable and @Mocked in JMockit?

Read more ›



01: JUnit with Mockito tutorial step by step

JUnit with Mockito tutorial step by step. Mockito is a popular mocking framework used along with JUnit. JMockit is more popular than Mockito.

Step 1: Create a simple maven project say “code-quality”

Step 2: Import into eclipse.

Read more ›



01: Mockito interview Q&As & examples on @Mock, @Spy, @Captor and @InjectMocks

Firstly, you need the pom.xml entry to bring in the Mockito library.

@InjectMocks

Firstly, annotate the Test class with @RunWith(MockitoJUnitRunner.class) as shown below. The @InjectMocks annotation creates an instance of the class and injects the mocks that are created with the @Mock (or @Spy) annotations into this instance.

Read more ›



02: JMockit interview Q&As & examples on Expectations & Verifications

This extends 01: JMockit interview Q&As on @Injectable Vs @Mocked Vs @Tested. The purpose of mocking objects is to return an expected value to test the logic of the class under test. This can be achieved with JMockit Expectations.

An expectation represents a set of invocations to a specific mocked method/constructor that is relevant for a given test.

Read more ›



02: Mockito interview Q&As & examples on @Spy

A spy in mockito is a partial mock where part of the object will be mocked and part will use real method invocations. You can create a Spy a number of different ways. Consider using mocks instead of spies wherever possible as having to use a Spy could indicate violating...

This content is for 100-Day-Full-Access, 200-Day-Full-Access, 365-Day-Full-Access, and 2-Year-Full-Access members only. Register 50+ Free Java FAQs 50+ Free Big Data FAQs
Already a member? Log in here


02: Unit testing with JUnit, Mockito & Spring

Step 1: Create a simple Maven project from a command-line. Just press enter for all the prompts.

Step 2: Import the above “simple-mocking-test” in the file system into eclipse. File –> Import –> “Existing Maven Projects” and then select the “simple-mocking-test”

Read more ›



03: JMockit interview Q&As & examples on initialising @Injectable fields

This extends 02: JMockit interview Q&As & examples on Expectations & Verifications. We have already discussed that a non-final instance field annotated as @Tested in the test class will be considered for automatic instantiation and injection, just before the execution of a test method. … Read more ›...

This content is for 100-Day-Full-Access, 200-Day-Full-Access, 365-Day-Full-Access, and 2-Year-Full-Access members only. Register 50+ Free Java FAQs 50+ Free Big Data FAQs
Already a member? Log in here


03: Mockito interview Q&As & examples on @Mock, Mockito.mock(…) and @MockBean

Unlike a @Spy where you are spying on a real object, when you mock, you are creating a complete mock or fake object. Q: What are the different ways to mock an object? A: There are 3 ways to mock a dependent class that is sed by the class under...

This content is for 100-Day-Full-Access, 200-Day-Full-Access, 365-Day-Full-Access, and 2-Year-Full-Access members only. Register 50+ Free Java FAQs 50+ Free Big Data FAQs
Already a member? Log in here


03: Mockito to fully mock the DAO layer

This extends Part 1: Unit testing with JUnit, Mockito & Spring by mocking the DAO layer with the Mockito framework. Step 1: Service and DAO layer interfaces and implementations Service Layer

DAO Layer This layer will be mocked by Mockito in the next step. … Read more ›...

This content is for 100-Day-Full-Access, 200-Day-Full-Access, 365-Day-Full-Access, and 2-Year-Full-Access members only. Register 50+ Free Java FAQs 50+ Free Big Data FAQs
Already a member? Log in here


04: JMockit interview Q&As & examples on @Injectable vs @Mocked

This extends 01: JMockit interview Q&As on @Injectable Vs @Mocked Vs @Tested. The purpose of mocking objects is to return an expected value to test the logic of the class under test. This can be achieved with JMockit Expectations. Q1. … Read more ›...

This content is for 100-Day-Full-Access, 200-Day-Full-Access, 365-Day-Full-Access, and 2-Year-Full-Access members only. Register 50+ Free Java FAQs 50+ Free Big Data FAQs
Already a member? Log in here


04: Mockito interview Q&As & examples – PowerMock for mocking private & static methods

Let’s look at step by step to understand how to mock private & static methods with PowerMock & JUnit. pom.xml Step 1: The pom.xml file should have Mockito & Powermock libraries.

Config & Service classes to test Step 2: The implementation classes that need to be tested. … Read...

This content is for 100-Day-Full-Access, 200-Day-Full-Access, 365-Day-Full-Access, and 2-Year-Full-Access members only. Register 50+ Free Java FAQs 50+ Free Big Data FAQs
Already a member? Log in here


04: Mockito partially mocking with @Spy

This extends Part 2: Mockito to fully mock the DAO layer by modifying the service layer to demonstrate partial mocking with “Mockito.spy”. In the “SimpleServiceImpl” we are interested in only testing the method “processUser(int id)” and want to mock the method “getUserById(int id)” by always returning “Sam”. So, … Read...

This content is for 100-Day-Full-Access, 200-Day-Full-Access, 365-Day-Full-Access, and 2-Year-Full-Access members only. Register 50+ Free Java FAQs 50+ Free Big Data FAQs
Already a member? Log in here


05: JMockit interview Q&As & examples on Partial Mocking with Expectations(Object..)

This extends Mockit interview Q&As & examples on @Injectable Vs @Mocked Vs @Capturing Vs @Tested. Why partial mocking? All methods that can be called on a mocked instance get mocked. This is appropriate for most tests, but in some situations we might need to select only certain methods to be...

This content is for 100-Day-Full-Access, 200-Day-Full-Access, 365-Day-Full-Access, and 2-Year-Full-Access members only. Register 50+ Free Java FAQs 50+ Free Big Data FAQs
Already a member? Log in here


1 2 3 4

300+ Java Interview FAQs

Tutorials on Java & Big Data