Last active
October 7, 2017 22:37
-
-
Save soichisumi/50ad0388a35a6f4488af4dc09efd08a4 to your computer and use it in GitHub Desktop.
mockito and powermockito test example
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
/** | |
* Created by souichi_sumi on 2017/09/28. | |
*/ | |
//unimplemented class | |
public class Hoge { | |
static String var1 = "hoge1"; | |
static String var2 = "hoge2"; | |
static String var3 = "hoge3"; | |
static String var4 = "hoge4"; | |
public String get12(){return "";} //todo return var1 + var2 | |
public String connectStrs(String str){return "";} // todo return var3 + str + var4 | |
public String retStr(){return "autowired!";} | |
private int conductMult(int a, int b){ return 0;} //todo return a * b | |
public int pubCond(int a, int b){return conductMult(a,b);} | |
private String retSimpleString(String a){return a;} | |
} |
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
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.Spy; | |
import org.powermock.api.mockito.PowerMockito; | |
import org.powermock.core.classloader.annotations.PrepareForTest; | |
import org.powermock.modules.junit4.PowerMockRunner; | |
import org.powermock.reflect.Whitebox; | |
import static org.mockito.Mockito.*; | |
/** | |
* Created by souichi_sumi on 2017/09/28. | |
*/ | |
//test unimplemented class | |
@RunWith(PowerMockRunner.class) | |
@PrepareForTest(Hoge.class) | |
public class HogeTest { | |
@Spy | |
Hoge target = new Hoge(); | |
@Before | |
public void setUp() throws Exception{ | |
String resGet12 = Hoge.var1 + Hoge.var2; | |
when(target.get12()).thenReturn(resGet12); | |
when(target.connectStrs(anyString())).then((i) -> | |
Hoge.var3 + i.getArgumentAt(0, String.class) + Hoge.var4 ); | |
PowerMockito.doAnswer((a)->a.getArgumentAt(0,int.class)*a.getArgumentAt(1,int.class)).when(target,"conductMult",anyInt(), anyInt()); | |
} | |
@Test | |
public void testGet12Mock() { | |
Assert.assertEquals(target.get12(), "hoge1hoge2"); | |
} | |
@Test | |
public void testConnectStrsMock() { | |
Assert.assertEquals(target.connectStrs("yoyoyo"),"hoge3yoyoyohoge4"); | |
} | |
@Test | |
public void testAutowired(){Assert.assertEquals("autowired!",target.retStr());} | |
@Test | |
public void testCondMult(){Assert.assertEquals(10,target.pubCond(2,5));} | |
@Test | |
public void testPrivateMethod()throws Exception{ | |
Assert.assertEquals("given string", Whitebox.invokeMethod(target,"retSimpleString", "given string")); | |
} | |
} |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>jo.yoyoyousei.mocktest</groupId> | |
<artifactId>MockTest</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<properties> | |
<powermock.version>1.7.1</powermock.version> | |
</properties> | |
<dependencies> | |
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all --> | |
<dependency> | |
<groupId>org.mockito</groupId> | |
<artifactId>mockito-all</artifactId> | |
<version>1.10.19</version> | |
<scope>test</scope> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/junit/junit --> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>4.12</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.powermock</groupId> | |
<artifactId>powermock-module-junit4</artifactId> | |
<version>${powermock.version}</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.powermock</groupId> | |
<artifactId>powermock-api-mockito</artifactId> | |
<version>${powermock.version}</version> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment