这是工厂/服务如何提供帮助的人为示例。它使用@Inject
,但原理适用于@Autowired
等。
用于测试的Spring 文档在这里。
想象一个IsSummerService
, 它提供了一种方法来告诉你现在是否是夏天:
public interface IsItSummerService {
public boolean isItSummerNow();
}
一个简单的第一个 impl 可能如下所示。这个 impl 使用new
关键字来管理它的Date
依赖关系,并@Inject
管理夏季月份范围。这里的Date
依赖类似于你创建一个新的 bean。
import java.util.*;
import javax.inject.Inject;
public class IsItSummerServiceImpl1 implements IsItSummerService {
@Inject int startMonth = Calendar.JUNE;
@Inject int endMonth = Calendar.SEPTEMBER;
public boolean isItSummerNow() {
return DateUtils.isDateBetweenMonths(new Date(), startMonth, endMonth);
}
}
第二个 impl 可以使用 DI 注入一个NowService
(或工厂)。该服务/工厂将取消对 的使用new
,并将创建新 bean的责任转交给服务/工厂。
import java.util.*;
import javax.inject.Inject;
public class IsItSummerServiceImpl2 implements IsItSummerService {
@Inject NowService nowService;
@Inject int startMonth = Calendar.JUNE;
@Inject int endMonth = Calendar.SEPTEMBER;
public boolean isItSummerNow() {
return DateUtils.isDateBetweenMonths(nowService.now(), startMonth, endMonth);
}
}
现在服务:
import java.util.Date;
public interface NowService {
public Date now();
}
那么现在每个测试有多难?无论您在哪里看到“假注入”,都可以通过 Spring 和测试上下文来实现。
的测试IsItSummerServiceImpl2
显然更好,更灵活。
import java.text.*;
import java.util.*;
import org.junit.*;
import static org.junit.Assert.*;
public class IsItSummerServiceTest {
@Test
public void testIsItSummerImpl1() {
IsItSummerService s = new IsItSummerServiceImpl1();
assertFalse(s.isItSummerNow());
// Test passes for May in the UK
// But what about when 'now' changes?
// We can't control the 'now' value effectively to test.
// assertFalse could pass/fail unpredictably.
// (Well, it actually *is* predictable when it passes/fails of course!)
}
@Test
public void testIsItSummerImpl2() {
// You would keep these tests separate, but for the sake of brevity.
IsItSummerService s = new IsItSummerServiceImpl2();
// Fake injection - set values directly. Use mocks/stubs/whatever.
((IsItSummerServiceImpl2)s).nowService = fakeNowService("01/01/2013");
assertFalse(s.isItSummerNow());
((IsItSummerServiceImpl2)s).nowService = fakeNowService("01/07/2013");
assertTrue(s.isItSummerNow());
}
@Test
public void testIsItSummerInSouthernHemisphere() {
// You would keep these tests separate, but for the sake of brevity.
IsItSummerService s = new IsItSummerServiceImpl2();
// Fake injection - set values directly. Use mocks/stubs/whatever.
((IsItSummerServiceImpl2)s).startMonth = Calendar.DECEMBER;
((IsItSummerServiceImpl2)s).endMonth = Calendar.MARCH;
((IsItSummerServiceImpl2)s).nowService = fakeNowService("01/11/2013");
assertFalse(s.isItSummerNow());
((IsItSummerServiceImpl2)s).nowService = fakeNowService("01/12/2013");
assertTrue(s.isItSummerNow());
((IsItSummerServiceImpl2)s).nowService = fakeNowService("01/03/2013");
assertTrue(s.isItSummerNow());
((IsItSummerServiceImpl2)s).nowService = fakeNowService("01/04/2013");
assertFalse(s.isItSummerNow());
}
static class FakeNowService implements NowService {
Date now;
public FakeNowService(Date now) {
this.now = now;
}
public Date now() {
return now;
}
}
Date parseDate(String dateStr) {
try {
// UK date format
return new SimpleDateFormat("dd/MM/yyyy").parse(dateStr);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
NowService fakeNowService(String dateStr) {
return new FakeNowService(parseDate(dateStr));
}
}
因此,为了回到您的问题,通过从new
关键字转移对象创建,我们使应用程序更具可测试性。
new
如果您想在以后更改对象创建过程,则不希望使用。用过一次new
就差不多了。但是有了工厂,您可以随心所欲地改变创建过程。