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

相关推荐
Dnelic-19 小时前
Android 单元测试环境配置问题 Execution failed for task ‘:mergeDebugJavaResource‘.
android·人工智能·junit·单元测试·gradle·自学笔记
fensnote1 天前
Qt编译lua库并调用
qt·junit·lua
GDAL2 天前
lua入门教程:ipairs
开发语言·junit·lua
GDAL4 天前
lua入门教程 :模块和包
开发语言·junit·lua
风霜不见闲沉月7 天前
kong网关的使用
junit·kong
一名技术极客8 天前
Nginx 实现动态封禁IP,详细教程来了
tcp/ip·nginx·junit
菠萝地亚狂想曲9 天前
优雅的LUA数据记录方法-serpent序列化+LUA Table
开发语言·junit·lua
硬汉嵌入式10 天前
H7-TOOL的LUA小程序教程第17期:扩展驱动AD7606, ADS1256,MCP3421, 8路继电器和5路DS18B20(2024-11-01)
junit·小程序·lua
程序猿小D11 天前
第三百零六节 Log4j教程 - Log4j日志级别
junit·单元测试·log4j