我有一个需要一些的模块Depedency.是否可以注入模块本身?我意识到这有点鸡蛋和鸡蛋的情况......
例:
public class MyModule implements Module {
private final Dependency d_;
@Inject public MyModule(Dependency d) {
d_ = d;
}
public void configure(Binder b) { }
@Provides Something provideSomething() {
// this requires d_
}
}
Run Code Online (Sandbox Code Playgroud)
我想在这种情况下,解决方案是将@Provides方法转变为一个完整的Provider<Something>类.这显然是一个简化的例子; 我正在处理的代码有很多这样的@Provides方法,所以将它们分成各个Provider<...>类并引入一个模块来配置它们会增加相当多的混乱 - 我认为Guice是关于减少样板杂乱的一切?
也许这反映了我对Guice的相对苛刻,但我遇到了一些我很想做上述事情的案例.我肯定错过了什么...
Jinja2(在 Saltstack 中支持)似乎不支持正则表达式匹配,除非我遗漏了什么?许多利用 Jinja2 的框架,例如Ansible,都有对正则表达式过滤器的自定义支持。 在其他地方,人们被指示编写自定义过滤器。
向知情人士提出的一些问题可能有助于理解这种情况:
我需要公开一个 API(consume(sequence)如下),该 API 要求其参数sequence集合按照以下摘录进行排序:
interface Consumer<T> {
/**
* @param sequence: an *ordered* collection of Ts to be processed in order
*/
public void consume(Collection<T> sequence);
}
interface Producer<T> {
Collection<T> getSequence();
}
class Producer1<T> implements Producer<T> {
public List<T> getSequence() {
return new ArrayList<>();
}
}
class Producer2<T> implements Producer<T> {
public Deque<T> getSequence() {
return new LinkedList<>();
}
}
class Test {
void testMethod(Consumer<Long> consumer) {
consumer.consume(new Producer1<Long>().getSequence());
consumer.consume(new Producer2<Long>().getSequence());
}
}
Run Code Online (Sandbox Code Playgroud)
通常,人们会指定consume() …
我在EasyMock(3.1)类模拟方面遇到了一些困难.这应该适用于模拟部分类实现,我认为,这应该是理想的单元测试抽象基类,同时模拟丢失的方法.这是模式 - 一个可以立即识别的经典......
public interface Interface {
public void intfMethod();
}
public abstract class AbstractBase implements Interface {
public void otherMethod() {
// do some stuff we need to test...
intfMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
现在测试:
public class TestAbstractBase {
AbstractBase testInstance;
@Before
public void setUp() {
testInstance =
createMockBuilder(AbstractBase.class).addMockedMethod("intfMethod").createMock();
}
@Test
public void testOtherMethod() {
testInstance.intfMethod(); // expect this to be invoked on the mock...
replay(testInstance);
testInstance.otherMethod();
verify(testInstance);
}
}
Run Code Online (Sandbox Code Playgroud)
EasyMock似乎不喜欢这个.它抛出:
java.lang.IllegalArgumentException: Method not found (or private): intfMethod
at org.easymock.internal.MockBuilder.addMockedMethod(MockBuilder.java:78)
at …Run Code Online (Sandbox Code Playgroud)