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