How to mock just one static method in a class using Mockito?
How to tell a Mockito mock object to return something different the next time it is called?
mockito ArrayList<String> problem
Difference between @Mock and @InjectMocks
How do I unit test code which calls the Jersey Client API?
Forming Mockito "grammars"
How to mock new Date() in java using Mockito
How to Mock only LocalDate.now() using PowerMock
MockRestServiceServer simulate backend timeout in integration test
Mockito.any returns null
By default all methods are mocked. However, using Mockito.CALLS_REAL_METHODS
you can configure the mock to actually trigger the real methods excluding only one.
For example given the class Sample
:
class Sample{
static String method1(String s) {
return s;
}
static String method2(String s) {
return s;
}
}
If we want to mock only method1
:
@Test
public void singleStaticMethodTest(){
try (MockedStatic<Sample> mocked = Mockito.mockStatic(Sample.class,Mockito.CALLS_REAL_METHODS)) {
mocked.when(() -> Sample.method1(anyString())).thenReturn("bar");
assertEquals("bar", Sample.method1("foo")); // mocked
assertEquals("foo", Sample.method2("foo")); // not mocked
}
}
Be aware that the real Sample.method1()
will still be called. From Mockito.CALLS_REAL_METHODS
docs:
This implementation can be helpful when working with legacy code. When this implementation is used, unstubbed methods will delegate to the real implementation. This is a way to create a partial mock object that calls real methods by default. ...
Note 1: Stubbing partial mocks using
when(mock.getSomething()).thenReturn(fakeValue)
syntax will call the real method. For partial mock it's recommended to usedoReturn
syntax.
So if you don't want to trigger the stubbed static method at all, the solution would be to use the syntax doReturn
(as the doc suggests) but for static methods is still not supported:
@Test
public void singleStaticMethodTest() {
try (MockedStatic<Sample> mocked = Mockito.mockStatic(Sample.class,Mockito.CALLS_REAL_METHODS)) {
doReturn("bar").when(mocked).method1(anyString()); // Compilation error!
//...
}
}
About this there is an open issue, in particular check this comment.
Check out the latest blogs from LambdaTest on this topic:
People love to watch, read and interact with quality content — especially video content. Whether it is sports, news, TV shows, or videos captured on smartphones, people crave digital content. The emergence of OTT platforms has already shaped the way people consume content. Viewers can now enjoy their favorite shows whenever they want rather than at pre-set times. Thus, the OTT platform’s concept of viewing anything, anytime, anywhere has hit the right chord.
A good User Interface (UI) is essential to the quality of software or application. A well-designed, sleek, and modern UI goes a long way towards providing a high-quality product for your customers − something that will turn them on.
Hola Testers! Hope you all had a great Thanksgiving weekend! To make this time more memorable, we at LambdaTest have something to offer you as a token of appreciation.
In some sense, testing can be more difficult than coding, as validating the efficiency of the test cases (i.e., the ‘goodness’ of your tests) can be much harder than validating code correctness. In practice, the tests are just executed without any validation beyond the pass/fail verdict. On the contrary, the code is (hopefully) always validated by testing. By designing and executing the test cases the result is that some tests have passed, and some others have failed. Testers do not know much about how many bugs remain in the code, nor about their bug-revealing efficiency.
So, now that the first installment of this two fold article has been published (hence you might have an idea of what Agile Testing is not in my opinion), I’ve started feeling the pressure to explain what Agile Testing actually means to me.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Perform automation testing with Mockito on LambdaTest, the most powerful, fastest, and secure cloud-based platform to accelerate test execution speed.
Test Now