My question may be a tad misleading but i'm not sure of any other way to ask it. Basically I'm having trouble discerning the differences between 'dependency injection' and 'java composition'. They both involve using variables to access classes. With dependency injection you pass an already instantiated instance (class A) created outside of Class B into the class B via constructor. With Java composition it seems you create a new instance of Class A inside of Class B since Class B cannot exist without Class A. If I'm interpreting this wrong then I'm not understanding the design pattern difference between dependency injection and Java composition since you have to do the same thing to use variables to access other class objects.
Anywho, using mockito, how would I go about testing this Java composition pattern?
What i'm needing help with:
Class Bar
cannot exist without Class Foo
(Java Composition: Has-A). therefore, i create a new instance of Foo inside bar instead of injecting an already created instance of Foo
into Bar
(dependency injection).
How do I properly set this up in Mockito so that I can mock a class of Foo
inside Bar
? My pseudocode is below.
Create interface
public interface MyRequiredObjectsInterface {
public String stringOne();
public List<String> listOne();
}
Create Class 'Foo' that implements Interface
public class Foo implements MyRequiredObjectsInterface {
public Foo() {
//Empty for example
}
@override
public String stringOne() {
return "StackOverflow Help";
}
@override
public List<String> listOne() {
return new List<String>();
}
}
Create Class 'Bar' that cannot exist without class 'Foo'
public class Bar implements MyRequiredObjectsInterface {
Private Foo foo;
public Bar() {
*/
Java composition pattern. With dependency injection, I would just pass an instance
of foo through the constructor. I dont know why you dont do this with Java composition
pattern and if you do, then I dont understand the difference in design patterns with this.
/*
foo = new Foo();
}
@override
public String stringOne() {
return foo.stringOne();
}
@override
public List<String> listOne() {
return foo.listOne();
}
}
Ok, so now I've shown you the setup. In Mockito, how would I test this Java composition patter? The following code does not work because I cannot mock an instance of Foo
inside Bar
since the instance is created inside Bar
.
public class BarTest {
private Foo foo;
private Bar bar;
@Before
public void setUp()
this.bar = new Bar();
// There is no way to get this inside Bar...
this.foo = mock(Foo.class);
}
@test
public void testListOne() {
*/
NOTE how that if you look at the code above, it calls the object from Foo -- foo.listOne();
In a real world scenario, this would be more complicated logic here as Foo
may perform more complex logic that may need to be mocked out for testing
but to show my design of the application i used pseudocode to get the point
across to show what im struggling grasping with the Java composition design pattern.
This would run into errors since I cannot mock this with Java composition pattern.
*/
assertEquals(bar.listOne().equals(...)
}
Any thoughts on helping me solve my conundrum? Here are some references I used that didnt answer my question