Best Mockito code snippet using org.mockito.internal.configuration.injection.ConstructorInjectionTest.should_do_the_trick_of_instantiating
Source:ConstructorInjectionTest.java
...15 private Observer observer;16 private ConstructorInjectionTest.ArgConstructor whatever;17 private ConstructorInjection underTest;18 @Test19 public void should_do_the_trick_of_instantiating() throws Exception {20 boolean result = underTest.process(field("whatever"), this, newSetOf(observer));21 Assert.assertTrue(result);22 Assert.assertNotNull(whatever);23 }24 private static class ArgConstructor {25 ArgConstructor(Observer observer) {26 }27 }28}...
Unit test for method that calls multiple other methods using Mockito
jersey/Mockito: NullInsteadOfMockException on client.post call verification
Test class with a new() call in it with Mockito
powermock mocking constructor via whennew() does not work with anonymous class
Mockito: How to match any enum parameter
mocking protected method
How to mock a record with Mockito
Mocking Joda DateTime method using Mockito
Verifying sequence of private method calls in unit testing
How to mock ResultSet.next() method using Mockito
Is it poorly designed and implemented code to have a method that internally calls other methods?
Not really. But I'd say that, in this situation, the method that calls the others should be tested as if the others where not already tested separately. That is, it protects you from situations where your public methods stops calling the other ones without you noticing it.
Yes, it makes for (sometimes) a lot of test code. I believe that this is the point: the pain in writing the tests is a good clue that you might want to consider extracting those sub-methods into a separate class.
If I can live with those tests, then I consider that the sub-methods are not to be extracted yet.
What is the best practice and/or approach in writing a unit test for such a method (assuming it is itself a good idea) if one has chosen Mockito as their mocking framework?
I'd do something like that:
public class Blah {
public int publicMethod() {
return innerMethod();
}
int innerMethod() {
return 0;
}
}
public class BlahTest {
@Test
public void blah() throws Exception {
Blah spy = spy(new Blah());
doReturn(1).when(spy).innerMethod();
assertThat(spy.publicMethod()).isEqualTo(1);
}
}
Check out the latest blogs from LambdaTest on this topic:
In today’s data-driven world, the ability to access and analyze large amounts of data can give researchers, businesses & organizations a competitive edge. One of the most important & free sources of this data is the Internet, which can be accessed and mined through web scraping.
Mobile apps have been an inseparable part of daily lives. Every business wants to be part of the ever-growing digital world and stay ahead of the competition by developing unique and stable applications.
Automating testing is a crucial step in the development pipeline of a software product. In an agile development environment, where there is continuous development, deployment, and maintenance of software products, automation testing ensures that the end software products delivered are error-free.
Collecting and examining data from multiple sources can be a tedious process. The digital world is constantly evolving. To stay competitive in this fast-paced environment, businesses must frequently test their products and services. While it’s easy to collect raw data from multiple sources, it’s far more complex to interpret it properly.
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.
Get 100 minutes of automation test minutes FREE!!