From fd1a5796a91be4db4d30c2166c98e4a94a39046d Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Thu, 20 Oct 2016 23:10:14 +0200 Subject: [PATCH] Refactor Screenshot examples --- .../com/baeldung/printscreen/Screenshot.java | 13 +++++------- .../baeldung/printscreen/ScreenshotTest.java | 21 ++++--------------- .../ehcache/SquareCalculatorTest.java | 18 ++++++++-------- 3 files changed, 18 insertions(+), 34 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/printscreen/Screenshot.java b/core-java/src/main/java/com/baeldung/printscreen/Screenshot.java index 500860640a..7c29a61842 100644 --- a/core-java/src/main/java/com/baeldung/printscreen/Screenshot.java +++ b/core-java/src/main/java/com/baeldung/printscreen/Screenshot.java @@ -7,20 +7,17 @@ import java.io.File; public class Screenshot { - private String filePath; - private String filenamePrefix; - private String fileType; - private int timeToWait; + private final String filePath; + private final String filenamePrefix; + private final String fileType; - public Screenshot(String filePath, String filenamePrefix, - String fileType, int timeToWait) { + public Screenshot(String filePath, String filenamePrefix, String fileType) { this.filePath = filePath; this.filenamePrefix = filenamePrefix; this.fileType = fileType; - this.timeToWait = timeToWait; } - public void getScreenshot() throws Exception { + public void getScreenshot(int timeToWait) throws Exception { Thread.sleep(timeToWait); Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); Robot robot = new Robot(); diff --git a/core-java/src/test/java/com/baeldung/printscreen/ScreenshotTest.java b/core-java/src/test/java/com/baeldung/printscreen/ScreenshotTest.java index 5c0341cb36..895f514f4a 100644 --- a/core-java/src/test/java/com/baeldung/printscreen/ScreenshotTest.java +++ b/core-java/src/test/java/com/baeldung/printscreen/ScreenshotTest.java @@ -1,34 +1,21 @@ package com.baeldung.printscreen; import org.junit.After; -import org.junit.Before; import org.junit.Test; import java.io.File; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; public class ScreenshotTest { - private Screenshot screenshot; - private String filePath; - private String fileName; - private String fileType; - private File file; - - @Before - public void setUp() throws Exception { - filePath = ""; - fileName = "Screenshot"; - fileType = "jpg"; - file = new File(filePath + fileName + "." + fileType); - screenshot = new Screenshot(filePath, fileName, fileType, 2000); - } + private Screenshot screenshot = new Screenshot("", "Screenshot", "jpg"); + private File file = new File("Screenshot.jpg"); @Test public void testGetScreenshot() throws Exception { - screenshot.getScreenshot(); + screenshot.getScreenshot(2000); assertTrue(file.exists()); } diff --git a/spring-all/src/test/java/org/baeldung/ehcache/SquareCalculatorTest.java b/spring-all/src/test/java/org/baeldung/ehcache/SquareCalculatorTest.java index 875fd2a25e..ab7aebf7a1 100644 --- a/spring-all/src/test/java/org/baeldung/ehcache/SquareCalculatorTest.java +++ b/spring-all/src/test/java/org/baeldung/ehcache/SquareCalculatorTest.java @@ -1,20 +1,20 @@ package org.baeldung.ehcache; -import static org.junit.Assert.*; - import org.baeldung.ehcache.calculator.SquaredCalculator; import org.baeldung.ehcache.config.CacheHelper; import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + public class SquareCalculatorTest { - SquaredCalculator squaredCalculator = new SquaredCalculator(); - CacheHelper cacheHelper = new CacheHelper(); + private SquaredCalculator squaredCalculator = new SquaredCalculator(); + private CacheHelper cacheHelper = new CacheHelper(); @Before public void setup() { squaredCalculator.setCache(cacheHelper); - } @Test @@ -22,7 +22,7 @@ public class SquareCalculatorTest { for (int i = 10; i < 15; i++) { assertFalse(cacheHelper.getSquareNumberCache().containsKey(i)); System.out.println("Square value of " + i + " is: " - + squaredCalculator.getSquareValueOfNumber(i) + "\n"); + + squaredCalculator.getSquareValueOfNumber(i) + "\n"); } } @@ -31,13 +31,13 @@ public class SquareCalculatorTest { for (int i = 10; i < 15; i++) { assertFalse(cacheHelper.getSquareNumberCache().containsKey(i)); System.out.println("Square value of " + i + " is: " - + squaredCalculator.getSquareValueOfNumber(i) + "\n"); + + squaredCalculator.getSquareValueOfNumber(i) + "\n"); } - + for (int i = 10; i < 15; i++) { assertTrue(cacheHelper.getSquareNumberCache().containsKey(i)); System.out.println("Square value of " + i + " is: " - + squaredCalculator.getSquareValueOfNumber(i) + "\n"); + + squaredCalculator.getSquareValueOfNumber(i) + "\n"); } } }