Wie mock-Objekt mit dem Konstruktor einer Klasse?
Dies ist der test:
import static junit.framework.Assert.assertTrue;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.whenNew;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest( {ClassUnderTesting.class} )
public class ClassUnderTestingTest {
@Test
public void shouldInitializeMocks() throws Exception {
CollaboratorToBeMocked mockedCollaborator = mock(CollaboratorToBeMocked.class);
suppress(constructor(CollaboratorToBeMocked.class, InjectedIntoCollaborator.class));
whenNew(CollaboratorToBeMocked.class)
.withArguments(InjectedAsTypeIntoCollaborator.class)
.thenReturn(mockedCollaborator);
new ClassUnderTesting().methodUnderTesting();
assertTrue(true);
}
}
Diese sind die Klassen :
public class ClassUnderTesting {
public void methodUnderTesting() {
new CollaboratorToBeMocked(InjectedAsTypeIntoCollaborator.class);
}
}
public class CollaboratorToBeMocked {
public CollaboratorToBeMocked(Class<InjectedAsTypeIntoCollaborator> clazz) {
}
public CollaboratorToBeMocked(InjectedIntoCollaborator someCollaborator) {
}
public CollaboratorToBeMocked() {
}
}
public class InjectedAsTypeIntoCollaborator {
}
public class InjectedIntoCollaborator {
}
Dies ist der Fehler :
org.powermock.reflect.exceptions.TooManyConstructorsFoundException: Several matching constructors found, please specify the argument parameter types so that PowerMock can determine which method you're refering to.
Matching constructors in class CollaboratorToBeMocked were:
CollaboratorToBeMocked( InjectedIntoCollaborator.class )
CollaboratorToBeMocked( java.lang.Class.class )
Hier kommt die Frage: wie kann ich machen, PowerMock, herauszufinden, was Konstruktor zu suchen?
Die problematische Zeile wird die suppress
. Das ist, wo der Fehler herkommt.
Was passiert, wenn Sie entfernen die CollaboratorToBeMocked( java.lang.Class.class ) - Konstruktor? Funktioniert es dann?
du meinst, wenn ich entfernen die anderen Konstruktor... ja, wenn ich entfernen Sie den Konstruktor mit InjectedIntoCollaborator, es funktioniert
du meinst, wenn ich entfernen die anderen Konstruktor... ja, wenn ich entfernen Sie den Konstruktor mit InjectedIntoCollaborator, es funktioniert
InformationsquelleAutor Belun | 2011-02-10
Du musst angemeldet sein, um einen Kommentar abzugeben.
Vielleicht ist es zu spät für deine Frage. Ich traf Sie heute und fand die Lösung auf der folgenden url. Grundsätzlich müssen Sie Ihr argument geben wie.
http://groups.google.com/group/powermock/msg/347f6ef1fb34d946?pli=1
Hoffe es kann dir helfen. 🙂
InformationsquelleAutor Smartmarkey
Wusste ich nicht, von PowerMock, bis Sie schrieb Ihre Frage, aber haben etwas zu Lesen und fand diese in Ihre Dokumentation. Noch bin ich mir nicht wirklich sicher, wenn dir das hilft:
gefunden bei http://code.google.com/p/powermock/wiki/SuppressUnwantedBehavior
Ist es nicht das, was Sie wollte?
EDIT: Jetzt sehe ich, du hast schon versucht zu unterdrücken. Aber sind Sie sicher, dass Sie die unterdrückt anrufen? Ist nicht das erste argument von
constructor()
werden soll die Klasse, die Sie möchten surpress den Konstruktor?suppress(constructor(CollaboratorToBeMocked.class, InjectedIntoCollaborator.class));
ähm, mein test war schlecht. sobald ich Feste die test-Klasse, der test lief grün. so, das problem ist, jetzt, die unterdrückt werden. könnte ein bug sein...
Wenn Sie ein vollständiges Beispiel und immer noch nicht sehen, das erwartete Ergebnis, ich sollte auf jeden Fall einen bug-report für das Projekt.
InformationsquelleAutor Grzegorz Oledzki