クラス:
class MyProducer {
private Producer producer = null;
private ProducerCreator producerCreator = null;
public MyProducer() {
producerCreator = ProducerCreator.create(string name);
producer = producerCreator.createProducer();
}
public boolean foo() {
return producer.foo();
}
}
クラスProducerCreatorがないソースコードを外部パッケージからである。
@RunWith(PowerMockRunner.class)
@PrepareForTest(ProducerCreator.class)
public class ProducerTest {
@Test
public void fooTest() {
ProducerCreator producerCreatorMock = Mockito.mock(ProducerCreator.class);
PowerMockito.mockStatic(ProducerCreator.class);
PowerMockito.when(ProducerCreator.class, "createProducer", "name").thenReturn(producerCreatorMock);
:
public interface ProducerCreator {
static default ProducerCreator create(String name) {
return new ProducerCreatorImpl(...)
}
}
は、だから私はPowerMockitoと静的呼び出しを模擬しようとしています
(また、この試みた:
PowerMockito.when(ProducerCreator.create("name")).thenReturn(producerCreatorMock);
をしかし、それはすべての変更をしなかった)
MyProducer myProducer = new MyProducer();
assertTrue(myProducer.foo());
}
は、一般的に、私は次のような何かを得る:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
...
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
またはその他のエラーまたは望ましくない振る舞いをすることができます。 インターフェイスの「静的デフォルト」メソッドによる問題ですか? インターネットでそのような場合の例は見つかりませんでした。
UPD: 私は実際のコードを独自の方法で共有することはできません。
コンパイルし、外部パッケージから実際の静的なデフォルトの方法があります:
public interface ProducerCreator extends Closeable {
static default ProducerCreator create(String serviceUrl) throws ProducerCreatorException {
return ...
}
UPD 2:パッケージは、JNIパッケージです - CPPコードから生成...
:
そして、私のために働くあなたのテスト、私はいくつかの仮定を行いました。 –
確かに、確かに。私はコードを参照してください:) –
'static'と' default'の両方のインターフェースメソッドを持つことはできません。確かに、あなたがコードサンプルをコンパイルしようとしておらず、デューデリジェンスを行っていないことがわかります。 「コードが見える」とはどういう意味ですか?その応答はあなたの質問を無効にします。 http://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.4「メソッドが複数の修飾子で宣言されている場合、コンパイル時エラーです。抽象的な '、'デフォルト '、'静的な 'あなたに_real_コードを教えてください。あなたが示したものはコンパイルできません。 –