Refactor Spring with Lombok samples

This commit is contained in:
pivovarit 2017-02-04 06:15:54 +01:00
parent 896a193fb0
commit 4d5624161f
11 changed files with 28 additions and 29 deletions

View File

@ -6,13 +6,13 @@ import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
public class Apologizer {
public class ApologizeService {
private final Translator translator;
private final String message;
@Autowired
public Apologizer(Translator translator) {
public ApologizeService(Translator translator) {
this(translator, "sorry");
}

View File

@ -4,11 +4,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Fareweller {
public class FarewellService {
private final Translator translator;
public Fareweller(Translator translator) {
public FarewellService(Translator translator) {
this.translator = translator;
}

View File

@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Greeter {
public class GreetingService {
@Autowired
private Translator translator;

View File

@ -1,12 +1,11 @@
package com.baeldung.lombok;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@AllArgsConstructor
public class Thanker {
public class ThankingService {
private final Translator translator;

View File

@ -14,12 +14,12 @@ import static org.mockito.Mockito.when;
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = TestConfig.class)
public class ApologizerAutowiringTest {
public class ApologizeServiceAutowiringTest {
private final static String TRANSLATED = "TRANSLATED";
@Autowired
private Apologizer apologizer;
private ApologizeService apologizeService;
@Autowired
private Translator translator;
@ -27,7 +27,7 @@ public class ApologizerAutowiringTest {
@Test
public void apologizeWithTranslatedMessage() {
when(translator.translate("sorry")).thenReturn(TRANSLATED);
assertEquals(TRANSLATED, apologizer.apologize());
assertEquals(TRANSLATED, apologizeService.apologize());
}
}

View File

@ -6,7 +6,7 @@ import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ApologizerTest {
public class ApologizeServiceTest {
private final static String MESSAGE = "MESSAGE";
private final static String TRANSLATED = "TRANSLATED";
@ -14,8 +14,8 @@ public class ApologizerTest {
@Test
public void apologizeWithCustomTranslatedMessage() {
Translator translator = mock(Translator.class);
Apologizer apologizer = new Apologizer(translator, MESSAGE);
ApologizeService apologizeService = new ApologizeService(translator, MESSAGE);
when(translator.translate(MESSAGE)).thenReturn(TRANSLATED);
assertEquals(TRANSLATED, apologizer.apologize());
assertEquals(TRANSLATED, apologizeService.apologize());
}
}

View File

@ -17,7 +17,7 @@ import static org.mockito.Mockito.when;
public class FarewellAutowiringTest {
@Autowired
private Fareweller fareweller;
private FarewellService farewellService;
@Autowired
private Translator translator;
@ -26,6 +26,6 @@ public class FarewellAutowiringTest {
public void sayByeWithTranslatedMessage() {
String translated = "translated";
when(translator.translate("bye")).thenReturn(translated);
assertEquals(translated, fareweller.farewell());
assertEquals(translated, farewellService.farewell());
}
}

View File

@ -6,7 +6,7 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class FarewellerTest {
public class FarewellServiceTest {
private final static String TRANSLATED = "TRANSLATED";
@ -14,7 +14,7 @@ public class FarewellerTest {
public void sayByeWithTranslatedMessage() {
Translator translator = mock(Translator.class);
when(translator.translate("bye")).thenReturn(TRANSLATED);
Fareweller fareweller = new Fareweller(translator);
assertEquals(TRANSLATED, fareweller.farewell());
FarewellService farewellService = new FarewellService(translator);
assertEquals(TRANSLATED, farewellService.farewell());
}
}

View File

@ -14,10 +14,10 @@ import static org.mockito.Mockito.when;
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = TestConfig.class)
public class GreeterTest {
public class GreetingServiceTest {
@Autowired
private Greeter greeter;
private GreetingService greetingService;
@Autowired
private Translator translator;
@ -26,12 +26,12 @@ public class GreeterTest {
public void greetWithTranslatedMessage() {
String translated = "translated";
when(translator.translate("hello")).thenReturn(translated);
assertEquals(translated, greeter.greet());
assertEquals(translated, greetingService.greet());
}
@Test(expected = NullPointerException.class)
public void throwWhenInstantiated() {
Greeter greeter = new Greeter();
greeter.greet();
GreetingService greetingService = new GreetingService();
greetingService.greet();
}
}

View File

@ -14,10 +14,10 @@ import static org.mockito.Mockito.when;
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = TestConfig.class)
public class ThankerAutowiringTest {
public class ThankingServiceAutowiringTest {
@Autowired
private Thanker thanker;
private ThankingService thankingService;
@Autowired
private Translator translator;
@ -26,6 +26,6 @@ public class ThankerAutowiringTest {
public void thankWithTranslatedMessage() {
String translated = "translated";
when(translator.translate("thank you")).thenReturn(translated);
assertEquals(translated, thanker.thank());
assertEquals(translated, thankingService.thank());
}
}

View File

@ -6,7 +6,7 @@ import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ThankerTest {
public class ThankingServiceTest {
private final static String TRANSLATED = "TRANSLATED";
@ -14,7 +14,7 @@ public class ThankerTest {
public void thankWithTranslatedMessage() {
Translator translator = mock(Translator.class);
when(translator.translate("thank you")).thenReturn(TRANSLATED);
Thanker thanker = new Thanker(translator);
assertEquals(TRANSLATED, thanker.thank());
ThankingService thankingService = new ThankingService(translator);
assertEquals(TRANSLATED, thankingService.thank());
}
}