Created
April 7, 2018 20:31
-
-
Save thetekst/363d4109d148c9ae446182f43bc48f93 to your computer and use it in GitHub Desktop.
powermock. мокаем конструктор внутри класса
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins { | |
id 'java' | |
id 'org.springframework.boot' version '1.5.10.RELEASE' | |
} | |
group 'ru.test' | |
version '1.0-SNAPSHOT' | |
buildDir = 'target' | |
sourceCompatibility = 1.8 | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
compile("org.springframework.boot:spring-boot-starter") | |
testCompile("cglib:cglib-nodep:3.2.4") | |
compileOnly 'org.projectlombok:lombok' | |
testCompile 'junit:junit:4.12' | |
testCompile 'org.codehaus.groovy:groovy-all:2.4.10' | |
testCompile 'org.hamcrest:hamcrest-core:1.3' | |
testCompile 'org.mockito:mockito-all:1.10.19' | |
testCompile 'org.powermock:powermock-module-junit4:1.7.3' | |
testCompile 'org.powermock:powermock-api-mockito:1.7.3' | |
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4' | |
} | |
task wrapper(type: Wrapper) { | |
gradleVersion = '4.4' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru; | |
import lombok.Data; | |
/** | |
* Created by Dmitry Tkachenko on 4/7/18 | |
*/ | |
@Data | |
public class Child { | |
private int age; | |
private String name; | |
public void init(int age, String name) { | |
this.age = age; | |
this.name = name; | |
System.out.println("---------ТЫ-НЕ-ДОЛЖЕН-УВИДЕТЬ-ЭТОГО-СООБЩЕНИЯ--------"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru; | |
import lombok.Data; | |
/** | |
* Created by Dmitry Tkachenko on 4/7/18 | |
*/ | |
@Data | |
public class Parent { | |
private Child child; | |
public void init(int age, String name) { | |
this.child = new Child(); | |
this.child.init(age, name); | |
System.out.println("-----E-N-D------"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru | |
import org.junit.runner.RunWith | |
import org.mockito.Matchers | |
import org.mockito.Mockito | |
import org.powermock.api.mockito.PowerMockito | |
import org.powermock.core.classloader.annotations.PrepareForTest | |
import org.powermock.modules.junit4.PowerMockRunner | |
import org.powermock.modules.junit4.PowerMockRunnerDelegate | |
import org.spockframework.runtime.Sputnik | |
import spock.lang.Specification | |
/** | |
* Created by Dmitry Tkachenko on 4/7/18 | |
*/ | |
@RunWith(PowerMockRunner.class) | |
@PowerMockRunnerDelegate(Sputnik.class) | |
@PrepareForTest([Parent.class]) | |
class ParentTest extends Specification { | |
Parent parent | |
def setup() { | |
// parent = Spy(Parent) // не будет работать! | |
parent = Mockito.spy(new Parent()) // или так Mockito.spy(Parent.class) | |
} | |
def test() { | |
setup: | |
Child childMock = Mockito.mock(Child.class) // не Child childMock = Mock() | |
// иначе получим исключение: | |
// org.mockito.exceptions.misusing.NotAMockException: | |
// Argument passed to when() is not a mock! | |
// Example of correct stubbing: | |
// doThrow(new RuntimeException()).when(mock).someMethod(); | |
PowerMockito.whenNew(Child.class).withNoArguments().thenReturn(childMock) | |
Mockito.doNothing().when(childMock).init(Matchers.any(Integer.class), Matchers.anyString()) // эту строку можно не писать. и так работает | |
when: | |
parent.init(3, 'rom') | |
then: | |
parent.child.age == 0 | |
parent.child.name == null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment