refactored and made the tests work
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
package com.example;
|
package com.example;
|
||||||
|
|
||||||
|
import com.example.constructorinjection.Cake;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
import com.example.constructorinjection.Cake;
|
|
||||||
|
|
||||||
@SpringBootApplication(scanBasePackages = { "com.example.constructorinjection", "com.example.dependency" })
|
@SpringBootApplication(scanBasePackages = { "com.example.constructorinjection", "com.example.dependency" })
|
||||||
public class ExampleApplicationCI {
|
public class ExampleApplicationCI {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public class ExampleApplicationFI {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
ApplicationContext context = SpringApplication.run(ExampleApplicationSI.class, args);
|
ApplicationContext context = SpringApplication.run(ExampleApplicationFI.class, args);
|
||||||
IceCream obj = context.getBean(IceCream.class);
|
IceCream obj = context.getBean(IceCream.class);
|
||||||
System.out.println("IceCream : " + obj.toString());
|
System.out.println("IceCream : " + obj.toString());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,13 +12,8 @@ public class Cake {
|
|||||||
private Flavor flavor;
|
private Flavor flavor;
|
||||||
|
|
||||||
Cake(Flavor flavor) {
|
Cake(Flavor flavor) {
|
||||||
|
Objects.requireNonNull(flavor);
|
||||||
// check if the required dependency is not null
|
this.flavor = flavor;
|
||||||
if (Objects.requireNonNull(flavor) != null) {
|
|
||||||
this.flavor = flavor;
|
|
||||||
} else {
|
|
||||||
throw new IllegalArgumentException("Cake cannot be created with null flavor object");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Flavor getFlavor() {
|
public Flavor getFlavor() {
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ import com.example.dependency.Topping;
|
|||||||
@Component
|
@Component
|
||||||
public class Sandwich {
|
public class Sandwich {
|
||||||
|
|
||||||
Topping toppings;
|
private Topping toppings;
|
||||||
Bread breadType;
|
private Bread breadType;
|
||||||
|
|
||||||
Sandwich(Topping toppings) {
|
Sandwich(Topping toppings) {
|
||||||
this.toppings = toppings;
|
this.toppings = toppings;
|
||||||
|
|||||||
@@ -7,15 +7,13 @@ import org.springframework.stereotype.Component;
|
|||||||
public class Pizza {
|
public class Pizza {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
Topping toppings;
|
private Topping toppings;
|
||||||
|
|
||||||
Pizza(Topping toppings) {
|
Pizza(Topping toppings) {
|
||||||
this.toppings = toppings;
|
this.toppings = toppings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public Topping getToppings() {
|
public Topping getToppings() {
|
||||||
System.out.println("Using field injection - " + this.toppings);
|
|
||||||
return toppings;
|
return toppings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,14 @@ import com.example.dependency.Topping;
|
|||||||
public class IceCream {
|
public class IceCream {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
Topping toppings;
|
private Topping toppings;
|
||||||
|
|
||||||
public Topping getToppings() {
|
public Topping getToppings() {
|
||||||
return toppings;
|
return toppings;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setToppings(Topping toppings) {
|
void setToppings(Topping toppings) {
|
||||||
|
System.out.println("Using setter injection - " + this.toppings);
|
||||||
this.toppings = toppings;
|
this.toppings = toppings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,24 +1,19 @@
|
|||||||
package com.example.test.constructorinjection;
|
package com.example.constructorinjection;
|
||||||
|
|
||||||
|
import com.example.ExampleApplicationCI;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.example.ExampleApplicationCI;
|
|
||||||
import com.example.constructorinjection.Cake;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringBootTest(classes = ExampleApplicationCI.class)
|
@SpringBootTest(classes = ExampleApplicationCI.class)
|
||||||
public class TestCakeClassConstructorInjection {
|
public class TestCakeClassConstructorInjection {
|
||||||
|
|
||||||
// @Mock
|
@Autowired
|
||||||
// Flavor flavor;
|
private Cake cake;
|
||||||
|
|
||||||
@InjectMocks
|
|
||||||
Cake cake;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConstructorInjection() {
|
public void testConstructorInjection() {
|
||||||
@@ -1,24 +1,25 @@
|
|||||||
package com.example.test.fieldinjection;
|
package com.example.fieldinjection;
|
||||||
|
|
||||||
|
import com.example.ExampleApplicationFI;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.example.ExampleApplicationSI;
|
import com.example.ExampleApplicationSI;
|
||||||
import com.example.fieldinjection.IceCream;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringBootTest(classes = ExampleApplicationSI.class)
|
@SpringBootTest(classes = ExampleApplicationFI.class)
|
||||||
public class TestCookieClassFieldInjection {
|
public class TestCookieClassFieldInjection {
|
||||||
|
|
||||||
@InjectMocks
|
@Autowired
|
||||||
IceCream iceCream;
|
private IceCream iceCream;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetterInjection() {
|
public void testFieldInjection() {
|
||||||
System.out.println(iceCream.toString());
|
System.out.println(iceCream.toString());
|
||||||
Assert.assertNotNull(iceCream.getToppings());
|
Assert.assertNotNull(iceCream.getToppings());
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.example.test.setterinjection;
|
package com.example.setterinjection;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -8,25 +8,17 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.example.ExampleApplicationSI;
|
import com.example.ExampleApplicationSI;
|
||||||
import com.example.setterinjection.Cake;
|
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@SpringBootTest(classes = ExampleApplicationSI.class)
|
@SpringBootTest(classes = ExampleApplicationSI.class)
|
||||||
public class TestCakeClassSetterInjection {
|
public class TestCakeClassSetterInjection {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
Cake cake;
|
private Cake cake;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSetterInjection() {
|
public void testSetterInjection() {
|
||||||
String testColor = cake.getFlavor().getColor();
|
Assert.assertNotNull(cake.getFlavor());
|
||||||
Assert.assertEquals(testColor, " White ");
|
Assert.assertNotNull(cake.getToppings());
|
||||||
String toppingsName=cake.getToppings().getToppingName();
|
|
||||||
|
|
||||||
//check if the dependency is not null
|
|
||||||
if(toppingsName!=null) {
|
|
||||||
Assert.assertEquals(toppingsName, "gems");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user