diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1f94fa7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +.gradle +build diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2066b7c --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: java + +jdk: +- oraclejdk8 + +sudo: false +addons: + apt: + packages: + - oracle-java8-installer \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..0dba262 --- /dev/null +++ b/build.gradle @@ -0,0 +1,16 @@ +group 'ru.spbau.shevchenko' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile "org.mockito:mockito-core:2.+" + testCompile group: 'junit', name: 'junit', version: '4.11' + compile group: 'org.jetbrains', name: 'annotations', version: '13.0' +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..ca78035 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..e036b6c --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Thu May 18 13:38:39 MSK 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..27309d9 --- /dev/null +++ b/gradlew @@ -0,0 +1,164 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..832fdb6 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..417a90b --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'XUnit' + diff --git a/src/main/java/annotations/XAfter.java b/src/main/java/annotations/XAfter.java new file mode 100644 index 0000000..1686ec2 --- /dev/null +++ b/src/main/java/annotations/XAfter.java @@ -0,0 +1,14 @@ +package annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation for methods that will be run before each test method invocation. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface XAfter { +} \ No newline at end of file diff --git a/src/main/java/annotations/XAfterClass.java b/src/main/java/annotations/XAfterClass.java new file mode 100644 index 0000000..b96b4eb --- /dev/null +++ b/src/main/java/annotations/XAfterClass.java @@ -0,0 +1,15 @@ +package annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation for methods that will be run after running test methods of this class. + */ + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface XAfterClass { +} \ No newline at end of file diff --git a/src/main/java/annotations/XBefore.java b/src/main/java/annotations/XBefore.java new file mode 100644 index 0000000..5c94dbd --- /dev/null +++ b/src/main/java/annotations/XBefore.java @@ -0,0 +1,15 @@ +package annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation for methods that will be run after each test method invocation. + */ + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface XBefore { +} \ No newline at end of file diff --git a/src/main/java/annotations/XBeforeClass.java b/src/main/java/annotations/XBeforeClass.java new file mode 100644 index 0000000..34c69c2 --- /dev/null +++ b/src/main/java/annotations/XBeforeClass.java @@ -0,0 +1,15 @@ +package annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation for methods that will be run before running test methods of this class. + */ + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface XBeforeClass { +} \ No newline at end of file diff --git a/src/main/java/annotations/XTest.java b/src/main/java/annotations/XTest.java new file mode 100644 index 0000000..b9017b1 --- /dev/null +++ b/src/main/java/annotations/XTest.java @@ -0,0 +1,33 @@ +package annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation for methods that are to be tested. + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface XTest { + /** + * Reason to ignore this test method. Empty string if it should not be ignored. + * @return reason why this test method should be ignored or empty string if it shouldn't + */ + String ignore() default ""; + + /** + * Expected exception or {@link NoException} if no exception is expected. + * @return class of exception that is expected to be thrown + */ + Class expected() default NoException.class; + + /** + * Stub class for {@link XTest#expected()} methods that don't throw any exception. + */ + final class NoException extends Throwable { + private NoException() { + } + } +} \ No newline at end of file diff --git a/src/main/java/annotations/package-info.java b/src/main/java/annotations/package-info.java new file mode 100644 index 0000000..c9c171f --- /dev/null +++ b/src/main/java/annotations/package-info.java @@ -0,0 +1,5 @@ +/** + * Provides annotations for test classes. + */ + +package annotations; diff --git a/src/main/java/xunit/IllegalTestClassException.java b/src/main/java/xunit/IllegalTestClassException.java new file mode 100644 index 0000000..aa7ad9f --- /dev/null +++ b/src/main/java/xunit/IllegalTestClassException.java @@ -0,0 +1,19 @@ +package xunit; + + +/** + * Exception that is thrown if test class or its methods are malformed. + */ +@SuppressWarnings("WeakerAccess") +public class IllegalTestClassException extends Exception { + IllegalTestClassException() { + } + + IllegalTestClassException(String s) { + super(s); + } + + IllegalTestClassException(String s, Exception e) { + super(s, e); + } +} diff --git a/src/main/java/xunit/XUnit.java b/src/main/java/xunit/XUnit.java new file mode 100644 index 0000000..f409d33 --- /dev/null +++ b/src/main/java/xunit/XUnit.java @@ -0,0 +1,213 @@ +package xunit; + +import annotations.*; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.PrintWriter; +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Class that provides method to test all methods of some class annotated with @XTest + */ +@SuppressWarnings("WeakerAccess") +public class XUnit { + @NotNull + private final PrintWriter logWriter; + + /** + * Creates new instance that will log itself's execution with given object. + * @param logWriter object that to be used for logging + */ + public XUnit(@NotNull PrintWriter logWriter) { + this.logWriter = logWriter; + } + + /** + * Runs all methods of given class that are annotated wit @XTest. + * + * Before running any of these methods @BeforeClass methods are invoked. + * After running all of these methods @AfterClass methods are invoked. + * Before running each of these methods @Before methods are invoked. + * After running each of these methods @After methods are invoked. + * @param className name of class to be tested + * @throws IllegalTestClassException if given class is malformed or can't be loaded + */ + public void test(@NotNull String className) throws IllegalTestClassException { + logWriter.println("Testing class " + className + "..."); + Class testedClass = getTestedClass(className); + List beforeClass = getAnnotatedMethods(testedClass, XBeforeClass.class); + assertStaticParameterless(beforeClass); + List afterClass = getAnnotatedMethods(testedClass, XAfterClass.class); + assertStaticParameterless(afterClass); + List beforeMethods = getAnnotatedMethods(testedClass, XBefore.class); + assertInstanceParameterless(beforeMethods); + List afterMethods = getAnnotatedMethods(testedClass, XAfter.class); + assertInstanceParameterless(afterMethods); + List testMethods = getAnnotatedMethods(testedClass, XTest.class); + assertInstanceParameterless(testMethods); + + Object testedClassInstance = getTestedInstance(testedClass); + test(testedClassInstance, testMethods, beforeClass, afterClass, beforeMethods, afterMethods); + } + + private void assertInstanceParameterless(@NotNull List methods) throws IllegalTestClassException { + if (methods.stream().anyMatch(method -> Modifier.isStatic(method.getModifiers()))) { + throw new IllegalTestClassException("XBefore/XAfter methods should not be static!"); + } + assertParameterless(methods); + } + + private void assertStaticParameterless(@NotNull List methods) throws IllegalTestClassException { + if (methods.stream().anyMatch(method -> !Modifier.isStatic(method.getModifiers()))) { + throw new IllegalTestClassException("XBefore/XAfter class methods should be static!"); + } + assertParameterless(methods); + } + + private void assertParameterless(@NotNull List methods) throws IllegalTestClassException { + if (methods.stream().anyMatch(method -> method.getParameterCount() != 0)) { + throw new IllegalTestClassException("XBefore/XAfter methods should not have parameters!"); + } + } + + private void test(@NotNull Object testObject, @NotNull List testMethods, + @NotNull List beforeClass, @NotNull List afterClass, + @NotNull List beforeMethods, @NotNull List afterMethods) { + logWriter.println("Running XBeforeClass methods..."); + executeStaticMethods(beforeClass); + for (Method testMethod : testMethods) { + logWriter.println("Running test " + testMethod.getName()); + final String ignoreReason = testMethod.getAnnotation(XTest.class).ignore(); + if (!ignoreReason.isEmpty()) { + printIgnored(ignoreReason); + continue; + } + if (!executeMethods(testObject, beforeMethods)) { + logWriter.println("Failed to execute @XBefore methods"); + continue; + } + executeTestMethod(testObject, testMethod); + if (!executeMethods(testObject, afterMethods)) { + logWriter.println("Failed to execute @XAfter methods"); + } + } + logWriter.println("Running XAfterClass methods..."); + executeStaticMethods(afterClass); + } + + + private void executeTestMethod(@NotNull Object testObject, @NotNull Method testMethod) { + Throwable caughtException = null; + long startTime, finishTime; + startTime = System.currentTimeMillis(); + try { + testMethod.invoke(testObject); + } catch (IllegalAccessException e) { + logWriter.println("Method is private!"); + return; + } catch (InvocationTargetException e) { + caughtException = e.getCause(); + } + finishTime = System.currentTimeMillis(); + double executionTime = (finishTime - startTime) / 1000; + Class expectedException = testMethod.getAnnotation(XTest.class).expected(); + if (expectedException.equals(XTest.NoException.class)) { + if (caughtException == null) { + printSuccess(executionTime); + } + else { + printUnexpectedException(caughtException); + } + } + else { + if (caughtException == null) { + printExpectedButNotThrown(expectedException); + } + else { + if (expectedException.isAssignableFrom(caughtException.getClass())) { + printSuccess(executionTime); + } + else { + printExpectedButOtherThrown(expectedException, caughtException.getClass()); + } + } + } + } + + void printIgnored(String ignoreReason) { + logWriter.println("Ignored. Reason: " + ignoreReason); + } + void printExpectedButOtherThrown(@NotNull Class expectedException, + @NotNull Class caughtException) { + logWriter.println("XTest failed: expected " + expectedException + + " but got " + caughtException); + } + + void printExpectedButNotThrown(@NotNull Class expectedException) { + logWriter.println("XTest failed: expected " + expectedException); + } + + void printUnexpectedException(@NotNull Throwable caughtException) { + logWriter.println("XTest failed:"); + caughtException.printStackTrace(logWriter); + } + + void printSuccess(double executionTime) { + logWriter.println("XTest passed in " + executionTime + "s"); + } + + private boolean executeMethods(@Nullable Object testObject, @NotNull List methods) { + for (Method method : methods) { + try { + method.invoke(testObject); + } catch (IllegalAccessException | InvocationTargetException e) { + return false; + } + } + return true; + } + + private boolean executeStaticMethods(@NotNull List staticMethods) { + return executeMethods(null, staticMethods); + } + + private static List getAnnotatedMethods(@NotNull Class testedClass, @NotNull Class annotation) { + return Arrays.stream(testedClass.getMethods()) + .filter(method -> method.getAnnotation(annotation) != null) + .collect(Collectors.toList()); + } + + private Object getTestedInstance(@NotNull Class testedClass) throws IllegalTestClassException { + Constructor constructor; + try { + constructor = testedClass.getConstructor(); + } catch (NoSuchMethodException e) { + throw new IllegalTestClassException("Class should have constructor with no parameters!"); + } + try { + return constructor.newInstance(); + } catch (InstantiationException e) { + throw new IllegalTestClassException("Class should not be abstract!"); + } catch (IllegalAccessException e) { + throw new IllegalTestClassException("Class should have public constructor!"); + } catch (InvocationTargetException e) { + throw new IllegalTestClassException("Constructor has thrown an exception: " + e.getMessage(), e); + } + } + + private Class getTestedClass(@NotNull String className) throws IllegalTestClassException { + try { + return Class.forName(className); + } catch (ClassNotFoundException e) { + throw new IllegalTestClassException("Can't found " + className + " class!"); + } + } +} diff --git a/src/main/java/xunit/package-info.java b/src/main/java/xunit/package-info.java new file mode 100644 index 0000000..0618376 --- /dev/null +++ b/src/main/java/xunit/package-info.java @@ -0,0 +1,4 @@ +/** + * Contains XUnit main class and exceptions. + */ +package xunit; \ No newline at end of file diff --git a/src/test/java/xunit/XUnitTest.java b/src/test/java/xunit/XUnitTest.java new file mode 100644 index 0000000..338e4cd --- /dev/null +++ b/src/test/java/xunit/XUnitTest.java @@ -0,0 +1,110 @@ +package xunit; + +import annotations.XTest; +import org.junit.Before; +import org.junit.Test; +import org.mockito.AdditionalMatchers; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; + +import static org.mockito.Mockito.*; + +public class XUnitTest { + private XUnit xunit; + + @Before + public void initXunit() { + xunit = spy(new XUnit(mock(PrintWriter.class))); + } + @Test + public void testSuccessful() throws Exception { + xunit.test(SuccessfulClass.class.getName()); + verify(xunit, times(3)).printSuccess(AdditionalMatchers.geq(0d)); + verify(xunit, never()).printExpectedButNotThrown(any()); + verify(xunit, never()).printExpectedButOtherThrown(any(), any()); + verify(xunit, never()).printIgnored(any()); + verify(xunit, never()).printUnexpectedException(any()); + } + @Test + public void testUnexpected() throws Exception { + xunit.test(UnexpectedExceptionClass.class.getName()); + verify(xunit, never()).printSuccess(anyDouble()); + verify(xunit, never()).printExpectedButNotThrown(any()); + verify(xunit, never()).printExpectedButOtherThrown(any(), any()); + verify(xunit, never()).printIgnored(any()); + verify(xunit, times(1)).printUnexpectedException(any()); + } + @Test + public void testNotThrown() throws Exception { + xunit.test(NotThrownClass.class.getName()); + verify(xunit, never()).printSuccess(anyDouble()); + verify(xunit, times(1)).printExpectedButNotThrown(any()); + verify(xunit, never()).printExpectedButOtherThrown(any(), any()); + verify(xunit, never()).printIgnored(any()); + verify(xunit, never()).printUnexpectedException(any()); + } + @Test + public void testOtherThrown() throws Exception { + xunit.test(OtherThrownClass.class.getName()); + verify(xunit, never()).printSuccess(anyDouble()); + verify(xunit, never()).printExpectedButNotThrown(any()); + verify(xunit, times(1)).printExpectedButOtherThrown(any(), any()); + verify(xunit, never()).printIgnored(any()); + verify(xunit, never()).printUnexpectedException(any()); + } + @Test + public void testIgnored() throws Exception { + xunit.test(IgnoredClass.class.getName()); + verify(xunit, never()).printSuccess(anyDouble()); + verify(xunit, never()).printExpectedButNotThrown(any()); + verify(xunit, never()).printExpectedButOtherThrown(any(), any()); + verify(xunit, times(1)).printIgnored(any()); + verify(xunit, never()).printUnexpectedException(any()); + + } + + + @SuppressWarnings("WeakerAccess") + public static class SuccessfulClass { + @XTest + public void test1(){} + @XTest(expected = IOException.class) + public void test2() throws IOException { + throw new IOException(); + } + @XTest(expected = IOException.class) + public void test3() throws IOException { + throw new FileNotFoundException(); + } + } + + @SuppressWarnings("WeakerAccess") + public static class UnexpectedExceptionClass { + @XTest + public void test1() throws Exception { + throw new Exception(); + } + } + + @SuppressWarnings("WeakerAccess") + public static class NotThrownClass { + @XTest(expected = Exception.class) + public void test1() {} + } + + @SuppressWarnings("WeakerAccess") + public static class OtherThrownClass { + @XTest(expected = IOException.class) + public void test1() throws Exception { + throw new Exception(); + } + } + + @SuppressWarnings("WeakerAccess") + public static class IgnoredClass { + @XTest(ignore = "For testing purposes") + public void test1() {} + } +} \ No newline at end of file