Permalink
Browse files

* Adding code to simulate a custom exception object and the test case…

…s associated
  • Loading branch information...
mike-ensor committed Sep 26, 2012
0 parents commit c555fdcd36d4814d294fbdf1f9094363d54c817a
@@ -0,0 +1,10 @@
+
+# /
+/target
+/WEB-INF
+/*.iml
+/*.ipr
+/*.iws
+/.idea
+.gradle
+build
51 pom.xml
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.clickconcepts.exception</groupId>
+ <artifactId>custom-exception-testing</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ <name>${project.artifactId}</name>
+ <description>Simple Server</description>
+ <url>http://www.ensor.cc/</url>
+
+ <organization>
+ <name>Click Concepts</name>
+ <url>http://www.clickconcepts.com</url>
+ </organization>
+
+ <developers>
+ <developer>
+ <id>Mike!</id>
+ <name>Mike Ensor</name>
+ <email>mike@clickconcepts.com</email>
+ <organization>Click Concepts</organization>
+ <organizationUrl>http://www.ensor.cc</organizationUrl>
+ <roles>
+ <role>Principal Architect</role>
+ </roles>
+ <timezone>-7</timezone>
+ </developer>
+ </developers>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+ </properties>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit-dep</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-core</artifactId>
+ <version>1.3.RC2</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
@@ -0,0 +1,16 @@
+package com.clickconcepts.exception;
+
+public class CustomException extends RuntimeException {
+
+ public static final String BAD_VERSION_CODE = "1.2.3.4";
+
+ private String errorCode;
+
+ public CustomException(String errorCode) {
+ this.errorCode = errorCode;
+ }
+
+ public String getErrorCode() {
+ return errorCode;
+ }
+}
@@ -0,0 +1,15 @@
+package com.clickconcepts.service;
+
+import com.clickconcepts.exception.CustomException;
+
+public class TypicalBusinessService {
+
+ public static final String KNOWN_VERSION_ERROR = "v12.53.1";
+
+ public String getOurBusinessName(String version) {
+ if (KNOWN_VERSION_ERROR.equals(version)) {
+ throw new CustomException(CustomException.BAD_VERSION_CODE);
+ }
+ return "Ah Yeah!";
+ }
+}
@@ -0,0 +1,31 @@
+package com.clickconcepts.matcher;
+
+import com.clickconcepts.exception.CustomException;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+
+public class CustomCodeMatcher extends TypeSafeMatcher<CustomException> {
+
+ public static CustomCodeMatcher hasCode(String item) {
+ return new CustomCodeMatcher(item);
+ }
+
+ private String foundErrorCode;
+ private String expectedErrorCode;
+
+ private CustomCodeMatcher(String expectedErrorCode) {
+ this.expectedErrorCode = expectedErrorCode;
+ }
+
+ @Override
+ protected boolean matchesSafely(final CustomException exception) {
+ this.foundErrorCode = exception.getErrorCode();
+ return foundErrorCode.equalsIgnoreCase(expectedErrorCode);
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendValue(foundErrorCode).appendText(" was not found instead of ").appendValue(expectedErrorCode);
+ }
+}
+
@@ -0,0 +1,32 @@
+package com.clickconcepts.service;
+
+import com.clickconcepts.exception.CustomException;
+import com.clickconcepts.matcher.CustomCodeMatcher;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+public class TypicalBusinessServiceTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void justTheExceptionMatcher() {
+ thrown.expect(CustomCodeMatcher.hasCode("12345"));
+ throw new CustomException("12345");
+ }
+
+ @Test
+ public void getOurBusinessName_noException() {
+ TypicalBusinessService service = new TypicalBusinessService();
+ service.getOurBusinessName("12345");
+ }
+
+ @Test
+ public void getOurBusinessName_exception() {
+ thrown.expect(CustomCodeMatcher.hasCode(CustomException.BAD_VERSION_CODE));
+ TypicalBusinessService service = new TypicalBusinessService();
+ service.getOurBusinessName(TypicalBusinessService.KNOWN_VERSION_ERROR);
+ }
+}

0 comments on commit c555fdc

Please sign in to comment.