Refactor Screenshot examples

This commit is contained in:
Grzegorz Piwowarek 2016-10-20 23:10:14 +02:00
parent cdbd83e851
commit fd1a5796a9
3 changed files with 18 additions and 34 deletions

View File

@ -7,20 +7,17 @@ import java.io.File;
public class Screenshot { public class Screenshot {
private String filePath; private final String filePath;
private String filenamePrefix; private final String filenamePrefix;
private String fileType; private final String fileType;
private int timeToWait;
public Screenshot(String filePath, String filenamePrefix, public Screenshot(String filePath, String filenamePrefix, String fileType) {
String fileType, int timeToWait) {
this.filePath = filePath; this.filePath = filePath;
this.filenamePrefix = filenamePrefix; this.filenamePrefix = filenamePrefix;
this.fileType = fileType; this.fileType = fileType;
this.timeToWait = timeToWait;
} }
public void getScreenshot() throws Exception { public void getScreenshot(int timeToWait) throws Exception {
Thread.sleep(timeToWait); Thread.sleep(timeToWait);
Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Robot robot = new Robot(); Robot robot = new Robot();

View File

@ -1,34 +1,21 @@
package com.baeldung.printscreen; package com.baeldung.printscreen;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import static org.junit.Assert.*; import static org.junit.Assert.assertTrue;
public class ScreenshotTest { public class ScreenshotTest {
private Screenshot screenshot; private Screenshot screenshot = new Screenshot("", "Screenshot", "jpg");
private String filePath; private File file = new File("Screenshot.jpg");
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);
}
@Test @Test
public void testGetScreenshot() throws Exception { public void testGetScreenshot() throws Exception {
screenshot.getScreenshot(); screenshot.getScreenshot(2000);
assertTrue(file.exists()); assertTrue(file.exists());
} }

View File

@ -1,20 +1,20 @@
package org.baeldung.ehcache; package org.baeldung.ehcache;
import static org.junit.Assert.*;
import org.baeldung.ehcache.calculator.SquaredCalculator; import org.baeldung.ehcache.calculator.SquaredCalculator;
import org.baeldung.ehcache.config.CacheHelper; import org.baeldung.ehcache.config.CacheHelper;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class SquareCalculatorTest { public class SquareCalculatorTest {
SquaredCalculator squaredCalculator = new SquaredCalculator(); private SquaredCalculator squaredCalculator = new SquaredCalculator();
CacheHelper cacheHelper = new CacheHelper(); private CacheHelper cacheHelper = new CacheHelper();
@Before @Before
public void setup() { public void setup() {
squaredCalculator.setCache(cacheHelper); squaredCalculator.setCache(cacheHelper);
} }
@Test @Test