在枚举中模拟注入
Mock an injection in an enum
对于一个框架,我试图在我的枚举中注入另一个 class 以提供一些环境信息。它似乎有效,但我不知道如何在单元测试中对此进行测试。请帮忙..
枚举:
public enum BestemmingBad implements Bestemming {
TEST("test"),
TEST2("test2");
BestemmingBad(final String value) {
this.value = value;
}
@Override
public String getUrl() {
final String jmsServer = Guice.createInjector().getInstance(MyEnvironmentConfig.class).retrieveJmsServerName();
return jmsServer + "/" + value;
}
}
我的测试;
public class BestemmingenTest extends ExtendedEasyMockSupport {
private Environment environmentMock;
@Before
public void setUp() {
environmentMock = createMock(Environment.class);
}
@Test
public void BestemmingBadTb5Test() throws UnknownHostException {
expect(environmentMock.getComputerName()).andReturn("TB5");
replayAll();
final BestemmingBad bestemming = BestemmingBad.TEST;
assertThat(bestemming.getUrl(), is("jms@testserver@jms1/test"));
}
private class TestModule extends TestGuiceModule {
@Provides
public Environment provideEnvironment() {
return environmentMock;
}
}
你不应该 "inject" 进入 enum
而是使用一个很好的旧 switch
语句,或者将您的注入点移动到真正可注入的东西中,例如 class。
为什么要注入 enum
?这没有道理。枚举是完全静态的。除非非常基本且一致,否则不应向枚举添加行为。你在这里所做的两者都不是。此外,调用您的方法会非常慢,因为 Guice 必须在每次调用时初始化 Injector
,这绝对不是您想要的。
您应该做的是将业务逻辑移出枚举本身:
public enum BestemmingBad implements Bestemming {
TEST("test"), TEST2("test2");
private final String value;
BestemmingBad(String value) { this.value = value; }
public String getValue() { return value; }
}
public class UrlGetter {
private String jmsServer;
@Inject UrlGetter(MyEnvironmentConfig config) {
jmsServer = config.retrieveJsmServerName();
}
public String getUrl(Bestemming bestemming) { // or BestemmingBad
return jmsServer + "/" + bestemming.getValue();
}
}
那么您的测试不需要任何复杂的东西:只需非常基本地测试 UrlGetter.getUrl(Bestemming)
。
对于一个框架,我试图在我的枚举中注入另一个 class 以提供一些环境信息。它似乎有效,但我不知道如何在单元测试中对此进行测试。请帮忙..
枚举:
public enum BestemmingBad implements Bestemming {
TEST("test"),
TEST2("test2");
BestemmingBad(final String value) {
this.value = value;
}
@Override
public String getUrl() {
final String jmsServer = Guice.createInjector().getInstance(MyEnvironmentConfig.class).retrieveJmsServerName();
return jmsServer + "/" + value;
}
}
我的测试;
public class BestemmingenTest extends ExtendedEasyMockSupport {
private Environment environmentMock;
@Before
public void setUp() {
environmentMock = createMock(Environment.class);
}
@Test
public void BestemmingBadTb5Test() throws UnknownHostException {
expect(environmentMock.getComputerName()).andReturn("TB5");
replayAll();
final BestemmingBad bestemming = BestemmingBad.TEST;
assertThat(bestemming.getUrl(), is("jms@testserver@jms1/test"));
}
private class TestModule extends TestGuiceModule {
@Provides
public Environment provideEnvironment() {
return environmentMock;
}
}
你不应该 "inject" 进入 enum
而是使用一个很好的旧 switch
语句,或者将您的注入点移动到真正可注入的东西中,例如 class。
为什么要注入 enum
?这没有道理。枚举是完全静态的。除非非常基本且一致,否则不应向枚举添加行为。你在这里所做的两者都不是。此外,调用您的方法会非常慢,因为 Guice 必须在每次调用时初始化 Injector
,这绝对不是您想要的。
您应该做的是将业务逻辑移出枚举本身:
public enum BestemmingBad implements Bestemming {
TEST("test"), TEST2("test2");
private final String value;
BestemmingBad(String value) { this.value = value; }
public String getValue() { return value; }
}
public class UrlGetter {
private String jmsServer;
@Inject UrlGetter(MyEnvironmentConfig config) {
jmsServer = config.retrieveJsmServerName();
}
public String getUrl(Bestemming bestemming) { // or BestemmingBad
return jmsServer + "/" + bestemming.getValue();
}
}
那么您的测试不需要任何复杂的东西:只需非常基本地测试 UrlGetter.getUrl(Bestemming)
。