All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.almondtools.testrecorder.dynamiccompile.TestsRunnableMatcher Maven / Gradle / Ivy

There is a newer version: 0.1.6
Show newest version
package com.almondtools.testrecorder.dynamiccompile;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

public class TestsRunnableMatcher extends TypeSafeDiagnosingMatcher {

	private DynamicClassCompiler compiler;

	public TestsRunnableMatcher() {
		compiler = new DynamicClassCompiler();
	}

	@Override
	public void describeTo(Description description) {
		description.appendText("should compile and run with success");
	}

	@Override
	protected boolean matchesSafely(String item, Description mismatchDescription) {
		try {
			Class clazz = compiler.compile(item);
			JUnitCore junit = new JUnitCore();
			Result result = junit.run(clazz);
			if (result.wasSuccessful()) {
				return true;
			}
			mismatchDescription.appendText("compiled successfully but got test failures : " + result.getFailureCount());
			return false;
		} catch (DynamicClassCompilerException e) {
			mismatchDescription.appendText(e.getMessage());
			for (String msg : e.getDetailMessages()) {
				mismatchDescription.appendText("\n\t" + msg);
			}
			return false;
		}
	}

	public static TestsRunnableMatcher testsRuns() {
		return new TestsRunnableMatcher();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy