Sunday, 25 June 2017

Why and how to set JAVA_HOME

Why to set JAVA_HOME?
Many applications like maven, elastic search etc., use JAVA_HOME property to find the location of the java they should use.

How to set JAVA_HOME?
Setting JAVA_HOME in windows

Right click on my computer -> properties.

Go to Advanced tab and click on ‘Environment Variables’ button.

In the System variables section, click on ‘New’ button.

Give the variable name as ‘JAVA_HOME’ and value to the location (For ex: C:\Program Files (x86)\Java\jdk1.8.0_102), where java is installed in your machine. Press Ok button.


That’s it you are done….:)

Setting JAVA_HOME in Linux Environment
Korn and bash shells:
export JAVA_HOME=jdk-install-dir

Bourne shell:
JAVA_HOME=jdk-install-dir
export JAVA_HOME

C shell:
setenv JAVA_HOME jdk-install-dir

You may like


How to Change Eclipse Default Web Browser

Open Eclipse

Go to Window -> Preferences





In the left pane, go to General -> Web Browser. 

In right side window, select the check box ‘Use external web browser’, select the xternal browser that installed in your machine and apply the settings.



You may like



Previous                                                    Next                                                    Home

Friday, 23 June 2017

Mock static non-void private methods

In this post, I am going to explain how to mock static non-void private methods.

Following snippet is used to mock the static non-void private method ‘readInputFromUI’.

String message = "Enter a number";
String methodToMock = "readInputFromUI";

PowerMock.mockStaticPartial(InputUtil.class, methodToMock);

PowerMock.expectPrivate(InputUtil.class, methodToMock, message).andReturn(10).times(1);

Following is the complete working application.

InputUtil.java
package com.sample.tests;

import javax.swing.JOptionPane;

public class InputUtil {

 public static int readInput(String message){
  return readInputFromUI(message);
 }
 
 private static int readInputFromUI(String message){
  int number = Integer.parseInt(JOptionPane.showInputDialog(message));
  return number;
 }
 
}


InputUtilTest.java
package com.sample.tests;

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({ InputUtil.class })
public class InputUtilTest {

 @Test
 public void mockVoidEvenMethod() throws Exception {
  String message = "Enter a number";
  String methodToMock = "readInputFromUI";

  PowerMock.mockStaticPartial(InputUtil.class, methodToMock);

  PowerMock.expectPrivate(InputUtil.class, methodToMock, message).andReturn(10).times(1);

  PowerMock.replayAll();

  int number = InputUtil.readInput(message);

  assertEquals(number, 10);
  PowerMock.verifyAll();

 }
}




Previous                                                 Next                                                 Home

Thursday, 22 June 2017

PowerMock, EasyMock: Mock static void private method

In this post, I am going to explain how to mock private static void methods. PowerMock provides 'expectPrivate' method to specify expectations on private methods using the method name.

Example
String message = "Enter Your Name";
String methodToMock = "readInputFromUI";
PowerMock.mockStaticPartial(InputUtil.class, methodToMock);
PowerMock.expectPrivate(InputUtil.class, methodToMock, message.toUpperCase()).times(5);

Above statement mock the private method 'readInputFromUI' and expect argument as "ENTER YOUR NAME" (in upper case).

Following is the complete working application.

InputUtil.java
package com.sample.tests;

import javax.swing.JOptionPane;

public class InputUtil {

 public static void readInput(String message){
  readInputFromUI(message.toUpperCase());
 }
 
 private static void readInputFromUI(String message){
  JOptionPane.showInputDialog(message);
 }
 
}


InputUtilTest.java
package com.sample.tests;

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({ InputUtil.class })
public class InputUtilTest {

 @Test
 public void mockVoidEvenMethod() throws Exception {
  String message = "Enter Your Name";
  String methodToMock = "readInputFromUI";

  PowerMock.mockStaticPartial(InputUtil.class, methodToMock);

  PowerMock.expectPrivate(InputUtil.class, methodToMock, message.toUpperCase()).times(5);

  PowerMock.replayAll();

  for (int i = 0; i < 5; i++) {
   InputUtil.readInput(message);
  }
  
  PowerMock.verifyAll();
  

 }
}




Previous                                                 Next                                                 Home

Wednesday, 21 June 2017

PowerMock, EasyMock: Mock static void parameterized method

In my previous post, I explained how to mock a static void method. In this post, I am going to explain how to mock static void parameterized method.

InputUtil.java
package com.sample.tests;

import javax.swing.JOptionPane;

public class InputUtil {

 public static void readInput(String message){
  readInputFromUI(message.toUpperCase());
 }
 
 public static void readInputFromUI(String message){
  JOptionPane.showInputDialog(message);
 }
 
}

Suppose, I want to test whether readInput method calls the readInputFromUI method and pass the argument in upper case or not.

Following snippet tests, whether ‘readInputFromUI’ is called with the string in upper case or not.

String message = "Enter Your Name";

PowerMock.mockStaticPartial(InputUtil.class, "readInputFromUI");

InputUtil.readInputFromUI(message.toUpperCase());
PowerMock.expectLastCall().times(5);

PowerMock.replayAll();


InputUtilTest.java
package com.sample.tests;

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({ InputUtil.class })
public class InputUtilTest {

 @Test
 public void mockVoidEvenMethod() {
  String message = "Enter Your Name";

  PowerMock.mockStaticPartial(InputUtil.class, "readInputFromUI");

  InputUtil.readInputFromUI(message.toUpperCase());
  PowerMock.expectLastCall().times(5);

  PowerMock.replayAll();

  for (int i = 0; i < 5; i++) {
   InputUtil.readInput(message);
  }
 }
}



Previous                                                 Next                                                 Home

Tuesday, 20 June 2017

PowerMock, EasyMock : Mock static void method

In this post, I am going to show how to mock static void methods. Sometimes you don’t want to call void methods, while testing a function (which in turn calling this void method). By using ‘expectLastCall’ method of PowerMock class, you can mock static void methods.


Following snippet mock the void method readInputFromUI of the class Number for 5 times.

PowerMock.mockStaticPartial(Number.class, "readInputFromUI");

Number.readInputFromUI();
PowerMock.expectLastCall().times(5);

PowerMock.replayAll();

Find the following working application.

Number.java
package com.sample.tests;

import javax.swing.JOptionPane;

public class Number {

 public static void readInput(){
  readInputFromUI();
 }
 
 public static void readInputFromUI(){
  JOptionPane.showInputDialog("Enter a number");
 }
 
}


NumberTest.java
package com.sample.tests;

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({ Number.class })
public class NumberTest {

 @Test
 public void mockVoidEvenMethod() {

  PowerMock.mockStaticPartial(Number.class, "readInputFromUI");

  Number.readInputFromUI();
  PowerMock.expectLastCall().times(5);

  PowerMock.replayAll();

  for (int i = 0; i < 5; i++) {
   Number.readInput();
  }
 }
}



Previous                                                 Next                                                 Home

Thursday, 15 June 2017

PowerMock, EasyMock : Partial mocking of static methods

In this post, I am going to explain how to mock static methods inside a class partially.

PowerMock class provides 'mockStaticPartial' method to mock the static methods partially. 

Suppose if your class has 10 static methods, but you want to mock only 2 of the static methods, then by using 'mockStaticPartial' method, you can mock only those two static methods.

public static synchronized void mockStaticPartial(Class<?> clazz, String... methodNames)
'clazz' represent the class that contains the static methods that should be mocked and 'methodNames' specifies the names of the methods that should be mocked.

Following step-by-step procedure explain, how to mock some static methods of a class.

Step 1: Prepare the class for testing
@RunWith(PowerMockRunner.class)
@PrepareForTest({ ProductInfo.class })
public class ProductInfoTest {
         ....
         ....
}

Step 2: By using ‘mockStaticPartial’ method of PowerMock class specify the methods to mock.

PowerMock.mockStaticPartial(ProductInfo.class, "getVersion");

Step 3: Set the expectation for mocked methods.
Version version = EasyMock.createMock(Version.class);
EasyMock.expect(version.getReleaseDate()).andReturn("DumyReleaseDate");
EasyMock.expect(version.getVersionNumber()).andReturn("001");

EasyMock.expect(ProductInfo.getVersion()).andReturn(version);

Step 4: Replay the expectations.
EasyMock.replay(version);
PowerMock.replayAll();

Following is the complete working application.

ProductInfo.java
package com.sample.tests;

public class ProductInfo {

 private static String versionNumber = "17.1";
 private static String releaseDate = "04-25-2017";
 
 private static Version version = new Version(versionNumber, releaseDate);

 public static String getProductName() {
  return "Java Tutorial";
 }

 public static Version getVersion() {
  return version;
 }

 public static String getSoftwareRequirements() {
  StringBuilder builder = new StringBuilder();

  return builder.append("Operating System").append(" : ").append("Windows").append("\n").append("RAM")
    .append(" : ").append("4GB").toString();

 }

}


Version.java
package com.sample.tests;

public class Version {
 private String versionNumber;
 private String releaseDate;

 public Version(String versionNumber, String releaseDate) {
  super();
  this.versionNumber = versionNumber;
  this.releaseDate = releaseDate;
 }

 public String getVersionNumber() {
  return versionNumber;
 }

 public String getReleaseDate() {
  return releaseDate;
 }

 public void setVersionNumber(String versionNumber) {
  this.versionNumber = versionNumber;
 }

 public void setReleaseDate(String releaseDate) {
  this.releaseDate = releaseDate;
 }

}


ProductInfoTest.java
package com.sample.tests;

import static org.junit.Assert.*;

import org.easymock.EasyMock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.api.easymock.PowerMock;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ ProductInfo.class })
public class ProductInfoTest {

 @Test
 public void testProductInfo() {
  PowerMock.mockStaticPartial(ProductInfo.class, "getVersion");

  Version version = EasyMock.createMock(Version.class);
  EasyMock.expect(version.getReleaseDate()).andReturn("DumyReleaseDate");
  EasyMock.expect(version.getVersionNumber()).andReturn("001");

  EasyMock.expect(ProductInfo.getVersion()).andReturn(version);

  EasyMock.replay(version);
  PowerMock.replayAll();

  String productName = ProductInfo.getProductName();
  Version versionReturned = ProductInfo.getVersion();

  assertEquals(versionReturned.getReleaseDate(), "DumyReleaseDate");
  assertEquals(versionReturned.getVersionNumber(), "001");

  assertEquals(productName, "Java Tutorial");
 }
}



Previous                                                 Next                                                 Home