Skip to content

Instantly share code, notes, and snippets.

@awilmore
awilmore / MockitoJUnitRunnerExampleTest.java
Created May 22, 2012 11:18
MockitoJUnitRunner setup example
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@awilmore
awilmore / SpringJUnitRunnerExampleTest.java
Created May 22, 2012 11:19
Spring JUnit Runner Using Test Context
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
"/spring/applicationContext.xml",
"/springtest/applicationContext-testDataSource.xml"})
public class SpringJUnitRunnerExampleTest {
...
@awilmore
awilmore / SpringJUnitIOResourceExampleTest.java
Created May 22, 2012 11:20
Loading SQL Resources Using Spring IO Resource Annotation
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({
@awilmore
awilmore / StatementLoader.java
Created May 22, 2012 11:22
SQL Statement Loader via Spring IO Resource
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
@awilmore
awilmore / ReflectionTestUtilsExampleTest.java
Created May 22, 2012 11:24
Populating Private Autowired Fields via Spring ReflectionTestUtils
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;
@RunWith(MockitoJUnitRunner.class)
public class ReflectionTestUtilsExampleTest {
@Mock private SomeDependency mockSomeDependency;
@awilmore
awilmore / ScalaExamples.scala
Created May 22, 2012 11:39
Various Scala Examples
// Find character with largest Unicode value
"Hello".reduceLeft((x, y) => if(x > y) x else y)
// Sum the unicode numeric value of the characters in "Hello World"
println("Hello World".foldLeft(0)((x, y) => x + y))
// Filter characters with even Unicode values
"Hello".filter(c => c % 2 == 0) // Filter characters with even Unicode values
"Hello".filter(_ % 2 == 0)
@awilmore
awilmore / PrimeSpec.scala
Created May 22, 2012 12:13
Scala Specs Example
import org.specs2.mutable._
import org.junit.runner._
import org.specs2.runner._
@RunWith(classOf[JUnitRunner])
class PrimeSpec extends Specification {
"The 'Prime' object" should {
"determine prime" in {
Prime.is(7919) must beTrue
@awilmore
awilmore / GetAndSetExampleTest
Created May 23, 2012 02:53
Andrew's Get And Set Tester
import static com.iaglimited.testing.util.GetterSetterTestProvider.shouldSetAndGet;
import org.junit.Test;
import com.iaglimited.testing.util.GetterSetterTestFilter;
public class GetAndSetExampleTest {
@Test
public void shouldSupportCorectGetterSetterBehaviour() {
@awilmore
awilmore / IOUtil.java
Created May 23, 2012 10:18
Fast nio InputStream to OutputStream Copy
package com.awilmore.ioutils;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
@awilmore
awilmore / Base64Util
Created May 23, 2012 10:19
Java Base64 Utils
package com.awilmore.stringutils;
import static javax.xml.bind.DatatypeConverter.parseBase64Binary;
import static javax.xml.bind.DatatypeConverter.printBase64Binary;
public class Base64Util {
public static String decode(String encoded) {
byte[] decoded = parseBase64Binary(encoded);
return new String(decoded);