I made a bit confusing statement at the end. I clarified a bit.
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 meant to say "(In fact, even if you do not
have any dependency injection scheme mentioned above, you can still inject
dependency to the target class via mocking.)" In other words, even if you
do not have the constructor method in which Address object is passed
as a constructor argument, you can still inject Address object to the
Person object via @InjectMocks. Of course, if you do this, the production
code will not work. :-)
-Sang
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
On Mon, Feb 13, 2017 at 6:58 PM, VikramSrinivasan Venkatakrishnan
<[email protected] <mailto:[email protected]>> wrote:
Please assist with my below question.
Let's say we are refactoring the Person class.
Before Person class:
Person.java has a method ,
public String getPersonAddressInfo(){
Address address = new Address ();
return address.getInfo();
}
After refactor Person class:
@Autowired
Address address;
public String getPersonAddressInfo(){
return address.getInfo();
}
Now , if we write a junit as below, mocking the address object,
@Mock
Address address;
@Inject Mock
Person person;
@setup{ required code}
@Test public void
should_return_adress_info_as_something(){
Mockito.when(address.getInfo()),thenReturn("Westbrough,MA");
assertThat(person.getPersonAddressInfo,
equalTo("Westborough,MA"));
}
The method should_return_adress_info_as_something works only for
the After class and not Before class. Why is this the case ? Is
it because we are creating a new object in the Before Person Class ?
Now, in the After class example that was given, let's say my prod
app is not on spring framework , how would i get an instance of
my address object ?
Kindly assist.
Thanks,
Vikram
--
-------------------------------------------------------------------
Sang Shin,[email protected]
http://www.linkedin.com/in/javapassion (Linkedin)
http://twitter.com/javapassion (Tweeter)
Life is worth living... with Passion!
----------------------------------------------------------------------
--
-------------------------------------------------------------------
Sang Shin, [email protected]
http://www.linkedin.com/in/javapassion (Linkedin)
http://twitter.com/javapassion (Tweeter)
Life is worth living... with Passion!
----------------------------------------------------------------------
--
You received this message because you are subscribed to the Google Groups
"JPassion.com: Java Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/jpassion_java.
For more options, visit https://groups.google.com/d/optout.