On 2/13/2017 7:16 PM, VikramSrinivasan
Venkatakrishnan wrote:
I can see in the dependency injection video , you
say the object cannot be mocked since it's created with that
class as it
I assume you meant it's created "within" that class..
beats the point of unit testing.
Correct.
Now lets say,
Person class is changed to support dependency injection (no
spring framework)
Dependency injection can happen in many different ways indeed
-via constructor
-via setter method
-via passing the dependency as a method argument
-Spring @Autowired
-CDI @Inject
Person.java
public class Person(){
Address address
public void Person (Address address){
this.address = address;
}
Yes, this is via constructor method
public String
getPersonAddressInfo(){
return address.getInfo();
}
}
I am assuming this should work, meaning we should be able
to Mock the Address obejct ?
Correct.
But lets say the Person object is created in a Request
Handler class. While testing RequestHandler we wont be able to
mock the objects right ? I hope you got my question. How do we
handle this ?
You do mocking only during testing. The code below is in
non-testing (production)
environment and has nothing to do with testing. (In fact, even if
you do not
have any dependency injection scheme mentioned above, you can still
inject
dependency to the target class.) I will demonstrate this during our
March 1st
codecamp if you want.
-S
RequestHandler.java
public void process (Person person){
Address address = new Address;
person = new Person (address);
person.getPersonAddressInfo();
}
Thanks,
Vikram
--
-------------------------------------------------------------------
Sang Shin, sangshi...@gmail.com
http://www.linkedin.com/in/javapassion (Linkedin)
http://twitter.com/javapassion (Tweeter)
Life is worth living... with Passion!
----------------------------------------------------------------------