Which statement best explains why advanced mocking is important when testing classes with complex dependencies?
Think about why isolating the class from its dependencies helps in testing.
Advanced mocking helps isolate the class under test by simulating its complex dependencies. This isolation ensures tests only verify the class's own logic, not the behavior of its dependencies.
What will be the output of this JUnit test using Mockito when the service method is called?
@RunWith(MockitoJUnitRunner.class) public class ServiceTest { @Mock private Dependency dependency; @InjectMocks private Service service; @Test public void testServiceMethod() { when(dependency.getData()).thenReturn("mocked data"); String result = service.process(); System.out.println(result); } } class Service { private Dependency dependency; public String process() { return "Processed: " + dependency.getData(); } } interface Dependency { String getData(); }
Consider what the mock returns when getData() is called.
The mock of Dependency returns "mocked data" when getData() is called. The service method concatenates this, so the output is "Processed: mocked data".
Which assertion correctly verifies that the mocked dependency's method was called exactly once during the test?
verify(dependency, ???).getData();
Check Mockito's syntax for verifying exact call counts.
Mockito uses times(1) to verify a method was called exactly once. once() is also valid and equivalent to times(1), called() is not a valid Mockito method, and atLeastOnce() allows more than one call.
Given this test code, why does it throw a NullPointerException?
public class ServiceTest { private Dependency dependency = new DependencyImpl(); private Service service = new Service(dependency); @Test public void testProcess() { when(dependency.getData()).thenReturn("mocked data"); String result = service.process(); assertEquals("Processed: mocked data", result); } } class Service { private Dependency dependency; public Service(Dependency dependency) { this.dependency = dependency; } public String process() { return "Processed: " + dependency.getData(); } } interface Dependency { String getData(); } class DependencyImpl implements Dependency { public String getData() { return "real data"; } }
Consider what happens when you call when() on a real object instead of a mock.
Calling when() on a real object causes a NullPointerException because Mockito expects a mock. The dependency is a real instance, so when() cannot intercept the call.
In a test with two mocked dependencies, which code snippet correctly verifies that dependencyA.methodA() was called before dependencyB.methodB()?
DependencyA dependencyA = mock(DependencyA.class); DependencyB dependencyB = mock(DependencyB.class); // code under test calls dependencyA.methodA() then dependencyB.methodB()
Look for Mockito's way to verify call order between mocks.
Mockito's InOrder class verifies the order of method calls on mocks. Option B correctly creates an InOrder object and verifies methodA() was called before methodB(). Option B does not check order. Option B uses a non-existent method verifyOrder(). Option B reverses the order.