WhiteBox
is a simplified reflection utility class used to access various internals of a
class.
You
can perform following actions by using WhiteBox class.
This blog is primarily focus on Java fundamentals and the libraries built on top of Java programming language. Most of the post are example oriented, hope you have fun in reading my blog....:)
package com.sample.model; public class MethodNotImplementedException extends RuntimeException { private static final long serialVersionUID = 1L; public MethodNotImplementedException() { super(); } public MethodNotImplementedException(String message) { super(message); } }
package com.sample.util; import com.sample.model.MethodNotImplementedException; public class StringUtil { public String toUpperAndRepeatStringTwice(String str) { String upperCase = str.toUpperCase(); boolean isLoggingSuccess = sendStringToLogger(str); if (isLoggingSuccess) { return upperCase + upperCase; } return null; } private boolean sendStringToLogger(String str) { throw new MethodNotImplementedException(); } }
package com.sample.util; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest({ StringUtil.class }) public class StringUtilTest { @Test public void toUpperAndRepeatStringTwice_sendStringToLogger_trueCheck() throws Exception { String methodToMock = "sendStringToLogger"; StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, methodToMock); String trueStr = "true"; /* Set expectations */ PowerMock.expectPrivate(stringUtil, methodToMock, trueStr).andReturn(true).times(1); PowerMock.replayAll(); String result = stringUtil.toUpperAndRepeatStringTwice(trueStr); assertEquals(result, "TRUETRUE"); PowerMock.verifyAll(); } @Test public void toUpperAndRepeatStringTwice_sendStringToLogger_falseCheck() throws Exception { String methodToMock = "sendStringToLogger"; StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, methodToMock); String falseStr = "false"; /* Set expectations */ PowerMock.expectPrivate(stringUtil, methodToMock, falseStr).andReturn(false).times(1); PowerMock.replayAll(); String result = stringUtil.toUpperAndRepeatStringTwice(falseStr); assertNull(result); PowerMock.verifyAll(); } }
package com.sample.model; public class MethodNotImplementedException extends RuntimeException { private static final long serialVersionUID = 1L; public MethodNotImplementedException() { super(); } public MethodNotImplementedException(String message) { super(message); } }
package com.sample.util; import com.sample.model.MethodNotImplementedException; public class StringUtil { public String toUpperAndRepeatStringTwice(String str) { String upperCase = str.toUpperCase(); sendStringToLogger(str); return upperCase + upperCase; } private void sendStringToLogger(String str){ throw new MethodNotImplementedException(); } }
package com.sample.util; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest({ StringUtil.class }) public class StringUtilTest { @Test public void toUpperAndRepeatStringTwice() throws Exception { String methodToMock = "sendStringToLogger"; StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, methodToMock); String input = "hello ptr"; PowerMock.expectPrivate(stringUtil, methodToMock, input).times(1); PowerMock.replayAll(); String result = stringUtil.toUpperAndRepeatStringTwice(input); assertEquals(result, "HELLO PTRHELLO PTR"); PowerMock.verifyAll(); } }
org.quartz.scheduler.instanceName: DefaultQuartzScheduler org.quartz.scheduler.rmi.export: false org.quartz.scheduler.rmi.proxy: false org.quartz.scheduler.wrapJobExecutionInUserTransaction: false org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount: 10 org.quartz.threadPool.threadPriority: 5 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true org.quartz.jobStore.misfireThreshold: 60000 org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
package com.sample.test; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class HelloWorldJob implements Job{ public void execute(JobExecutionContext jec) throws JobExecutionException { System.out.println("Hello World"); } }
package com.sample.test; import java.util.Date; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.SimpleTrigger; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.triggers.SimpleTriggerImpl; public class Test { private static String quartzConfigFileName = "C:\\Users\\Krishna\\quartz_tutorial\\myQuartzConfig.properties"; public static void main(String args[]) throws SchedulerException { SchedulerFactory schedulerFactory = new StdSchedulerFactory(quartzConfigFileName); // Retrieve a scheduler from schedule factory Scheduler scheduler = schedulerFactory.getScheduler(); // Creating JobDetailImpl and link to our Job class JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.setName("First Job"); jobDetail.setJobClass(HelloWorldJob.class); // Creating schedule time with trigger SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl(); simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY); simpleTrigger.setRepeatInterval(3000); simpleTrigger.setName("FirstTrigger"); // Start scheduler scheduler.start(); scheduler.scheduleJob(jobDetail, simpleTrigger); } }
appName=Demo Application to read resources in 'WEB-INF' directory version=1.0 author=Krishna
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/HelloWorld") public class HelloWorld extends HttpServlet { private static final long serialVersionUID = 1L; public HelloWorld() { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filePath = "/WEB-INF/app.properties"; String appName, version, author; try (InputStream is = request.getServletContext().getResourceAsStream(filePath)) { Properties properties = new Properties(); properties.load(is); appName = properties.getProperty("appName"); version = properties.getProperty("version"); author = properties.getProperty("author"); } response.getWriter().append("Application name : ").append(appName).append("<br />version : ").append(version) .append("<br />author : ").append(author); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.sample.model; public class MethodNotImplementedException extends RuntimeException { private static final long serialVersionUID = 1L; public MethodNotImplementedException() { super(); } public MethodNotImplementedException(String message) { super(message); } }
package com.sample.util; import com.sample.model.MethodNotImplementedException; public class StringUtil { public String toUpperAndRepeatStringTwice(String str) { String upperCase = str.toUpperCase(); sendStringToLogger(upperCase); return upperCase + upperCase; } public void sendStringToLogger(String str){ throw new MethodNotImplementedException(); } }
package com.sample.util; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest({ StringUtil.class }) public class StringUtilTest { @Test public void toUpperAndRepeatStringTwice() { StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, "sendStringToLogger"); String str = "HELLO PTR"; stringUtil.sendStringToLogger(str); PowerMock.expectLastCall().times(1); PowerMock.replayAll(); String result = stringUtil.toUpperAndRepeatStringTwice("hello ptr"); assertEquals(result, "HELLO PTRHELLO PTR"); } }