Skip to content

Instantly share code, notes, and snippets.

@bsharathchand
bsharathchand / building-maven-project-in-containers.md
Last active August 16, 2018 01:57
Building maven projects in docker containers

Building maven project in docker

> cd $GIT_HOME/projects/demo # navigate to project directory
> docker run --it --rm --name build -v $(PWD):/project -v $M2_HOME/.m2:/root/.m2 -w /project maven:3-jdk-7 mvn clean install

Anotomy of the docker run command

  • --it keeps the STDIN open and allocates a psuedo tty
  • --rm removes the container once the execution is complete
  • --name build assigns a name to the container
@bsharathchand
bsharathchand / clone-using-reflection-api-java.java
Created May 23, 2018 01:36
Object Cloning using Java Reflections
public Object cloneUsingReflections(Object value) {
// If Value is null then return
if (value == null)
return value;
// Use reflections to call the clone method on the object
try {
if (value instanceof Cloneable) {
Method cloneMethod;
cloneMethod = value.getClass().getMethod("clone");
@bsharathchand
bsharathchand / stringjoiner-merge-multiple-delimiters.md
Last active August 29, 2015 14:26
StringJoiner merge with different delimiters

StringJoiner class merge operation exampe

Merge 2 instance of StringJoiner will use the delimiters of both the instances.

    /**
     * Test of main method, of class StringJoinerSample.
     */
    @org.junit.Test
    public void mergingWithDifferentDelimiters() {
@bsharathchand
bsharathchand / string-join-operations.md
Last active August 29, 2015 14:26
String concatenation in Java 8

String concatenation before and after Java 8

Problem: To print a concatenated String like "Chennai and Hyderabad and Bangalore and Delhi" from an array ["Chennai", "Hyderabad", > "Bangalore", "Delhi"]

Before StringJoiner

final String delemiter = " and ";
String[] cities = new String[]{"Chennai", "Hyderabad", "Bangalore", "Delhi"};
@bsharathchand
bsharathchand / mockito-configuration.md
Last active August 29, 2015 14:16
Fix for Mockito ClassCastException When running TestSuites in JUnit

##Mockito framework throws ClassCastException when running TestSuites.

Writing TestCases using JUnit and Mockito and running them as testsuites at times throws exceptions.

I faced the issue when

  • There is a TestCase X that creates a stub of Class A and performs some tests
  • There is another TestCase Y; which will use Class A as is.
  • Running TestCase X, TestCase Y independantly then both will execute flawlessly.
  • When TestSuite Z is created by combining TestCase X, TestCase Y then only one TestCase class succeeds(Executed First) and other Fails.
@bsharathchand
bsharathchand / powermock-expectedexception-junit-rules.md
Last active May 3, 2018 06:19
Using ExpectedException Rule along with PowerMockRule in Junit 4

Using ExpectedException Rule with PowerMockRule in Junit 4

When using PowerMockRule for running the tests with PowerMockRunner then ExpectedExeption rule will not actually catch the exceptions, but throws it as an error. Inorder to make this work we will need to do this actions.

  1. Set PowerMockRule using @ClassRule
  2. Set ExpectedException using @Rule

Example code given below.

package test.com.novicehacks.autobot.learning.ssh;
@bsharathchand
bsharathchand / pass-by-value.md
Last active August 29, 2015 14:12
Java Pass By Value with Proof. Do you still think java uses Pass by reference for parameters then prove it!

Java Pass by value example

We would have read in many places that Java uses Pass By Value mechanism for parameter passing, but will still get confused when coming to passing object references to the methods. This gist is to give a more clear understanding of how java treats the object references with adequate proof of working code.

Java uses Pass-By-Value method no doubt about it!

When considering this topic, we need to discuss about 3 variations in java.

  1. Primitives
  2. Objects
@bsharathchand
bsharathchand / threadpool-usage.md
Last active August 29, 2015 14:12
Are the Initialization errors in child threads really eaten away by the Executor framework? --- No Definitely Not

Handling Initialization errors in Thread Pools using Executors f/w

If you are here, it means that there is a high chance that you spent alot of time debugging the initialization errors of your application when used with an ExecutorService thread pool in java; else you might just wanted to know more information to work with ExecutorServices in java.

Everyone of us knows that eating any exception without reporting is very dangerous, and should never be done.

  try{

> // do something

@bsharathchand
bsharathchand / junit-ordering.md
Created October 28, 2014 20:32
JUnit method ordering

Junit Test Execution Order

If any time, you have run junit tests and executed them on your code, you should have already observed that the test execution order for JUnit is not what you expected to be. The reason for this behaviour of Junit is because it uses Java Reflections to select and execute the test methods.

Did you ever wanted to execute your Unit tests in deterministic order? If yes we have got a way to do that in JUnit 4.

Inorder to exuecute the Unit tests in a fixed or deterministic order, you have got to decorate your TestClass with @FixedOrder annotation.

@FixedOrder annotation will take the parameter of the MethodSorters enum for sorting the test method execution order.

@bsharathchand
bsharathchand / resyncdate.md
Created October 23, 2014 22:34
Resync date & time in windows and unix system

##Synching date & time in Unix and Windows

At times, we might need to change the system time for working on some developmental and testing activities. Setting a new time is always easier, and pretty straight forward in many operating systems.

Have you ever wondered how to reset / resync the correct timestamp.

Yes, I thought so. We will try setting it back again to the correct time, instead of re-synching with the time server. Resetting the time using a time server is pretty easy, and you can do that just in a couple of steps, stated below.

Unix - (Ubuntu Flavour)