Junit test with mock

|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| /* * 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; } } |

相关推荐
霍格沃兹测试开发学社测试人社区2 天前
数据驱动与并行策略:用 JUnit 5 让软件测试更高效
软件测试·测试开发·junit
Dnelic-2 天前
【单元测试】【Android】JUnit 4 和 JUnit 5 的差异记录
android·junit·单元测试·android studio·自学笔记
霍格沃兹测试开发学社测试人社区2 天前
软件测试丨探索 JUnit 5 中的参数化与调度执行:提升软件测试的效率与灵活性
软件测试·测试开发·junit
Dnelic-2 天前
解决 Android 单元测试 No tests found for given includes:
android·junit·单元测试·问题记录·自学笔记
卷心菜是俺7 天前
Sping全面复习
java·开发语言·数据库·junit·java-ee·log4j·maven
qq_3129201110 天前
安装lua-nginx-module实现WAF功能
nginx·junit·lua
Dnelic-13 天前
Android 单元测试环境配置问题 Execution failed for task ‘:mergeDebugJavaResource‘.
android·人工智能·junit·单元测试·gradle·自学笔记
fensnote13 天前
Qt编译lua库并调用
qt·junit·lua
GDAL14 天前
lua入门教程:ipairs
开发语言·junit·lua