/* * This file was generated by the Gradle 'init' task. * * This generated file contains a sample Java project to get you started. * For more details take a look at the Java Quickstart chapter in the Gradle * User Manual available at https://docs.gradle.org/6.4/userguide/tutorial_java_projects.html */ apply plugin: 'java' apply plugin: 'java-library' apply plugin:'application' mainClassName = "gradletst.HelloWorld" repositories { mavenLocal() mavenCentral() } compileJava{ options.debug = true options.deprecation = true doLast { println "compileJava done..." } } dependencies { implementation 'org.slf4j:jcl-over-slf4j:1.7.36' testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' testImplementation 'org.mockito:mockito-junit-jupiter:4.11.0' testImplementation 'org.mockito:mockito-core:4.11.0' testImplementation group: 'net.bytebuddy', name: 'byte-buddy', version: '1.13.0' testImplementation group: 'net.bytebuddy', name: 'byte-buddy-agent', version: '1.13.0' testImplementation group: 'org.mockito', name: 'mockito-inline', version: '3.9.0' // END for test implementation group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.7' } test { useJUnitPlatform() } jar { manifest { attributes 'Main-Class': 'gradletst.HelloWorld' } } |
package gradletst.biz; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; import java.io.UnsupportedEncodingException; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.MockedConstruction; import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) public class JobManagerTest { @InjectMocks JobManager mgr; @Spy IdGenerator idWrapper; @Spy GroupGenerator nameGen; @Spy RunJob job; @Test public void test() throws UnsupportedEncodingException { System.out.println("running in test"); JobManager mgr= new JobManager(); mgr.run(); System.out.println(); } @Test public void testStatic() throws UnsupportedEncodingException { System.out.println("running in testStatic"); // static mock String retV = "MockSts"; MockedStatic<IdGenerator> msJob = Mockito.mockStatic(IdGenerator.class); msJob.when(() -> IdGenerator.generateId()).thenReturn(retV); mgr.run(); System.out.println(); msJob.close(); } @Test public void testInst() throws UnsupportedEncodingException { System.out.println("running in testInst"); // Instance is created inside method MockedConstruction<IdGenerator> mkCon = Mockito.mockConstruction(IdGenerator.class, (mock, context) -> when(mock.wrap(anyString())).thenReturn("MockIns")); mgr.run(); System.out.println(); mkCon.close(); } @Test public void testSpy() throws UnsupportedEncodingException { System.out.println("running in testSpy"); // mock one method when(nameGen.genGroupName()).thenReturn("SPY"); mgr.run(); System.out.println(); } } |
package gradletst.biz; import java.util.Random; public class IdGenerator { public static String generateId() { Random r=new Random(); String id = Integer.valueOf(r.nextInt()).toString() ; return id; } public String wrap(String id) { return "id:'"+id+"'"; } } package gradletst.biz; import java.io.UnsupportedEncodingException; public class GroupGenerator { String defaultName = "blue"; public String genGroupName() throws UnsupportedEncodingException { return defaultName; } public String wrapGroup(String name) { return "name:'" + name+"'"; } } package gradletst.biz; public class FullNameIntegrator { public String createStatement(String group,String id ) { return group + " " +id ; } } package gradletst.biz; import java.text.SimpleDateFormat; import java.util.Date; public class JobWrapper { public String wrap(String sts) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); String dt= sdf.format(new Date())+":"; return dt +" Job ["+sts +"] is running happily!"; } } package gradletst.biz; public class RunJob { public void run(String sts) { System.out.println("Start job ..."); System.out.println(sts); System.out.println("End job ..."); } } package gradletst.biz; import java.io.UnsupportedEncodingException; public class JobManager { RunJob job=new RunJob(); FullNameIntegrator svc =new FullNameIntegrator(); GroupGenerator nameGen=new GroupGenerator(); JobWrapper wrapper =new JobWrapper(); public void run() throws UnsupportedEncodingException { String id =IdGenerator.generateId(); IdGenerator idGen = new IdGenerator(); String wrapId = idGen.wrap(id); String group=nameGen.genGroupName(); String wrapGroup =nameGen.wrapGroup(group); String sts= svc.createStatement(wrapGroup, wrapId); String wrapSts = wrapper.wrap(sts); job.run(wrapSts); } public RunJob getJob() { return job; } public void setJob(RunJob job) { this.job = job; } public FullNameIntegrator getSvc() { return svc; } public void setSvc(FullNameIntegrator svc) { this.svc = svc; } } |
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » Junit test with mock
发表评论 取消回复