@Test public void testValidateTestMethods() throws Throwable { UnitilsMethodValidator methodValidator = new UnitilsMethodValidator(new TestClass(JustATestClass.class)); methodValidator.validateTestMethods(Test.class, false); Assert.assertEquals(getErrors().get(0).getMessage(), methodValidator.getErrors().get(0).getMessage()); Assert.assertEquals(getErrors().get(1).getMessage(), methodValidator.getErrors().get(1).getMessage()); Assert.assertEquals(getErrors().get(2).getMessage(), methodValidator.getErrors().get(2).getMessage()); Assert.assertEquals(getErrors().get(3).getMessage(), methodValidator.getErrors().get(3).getMessage()); }
@Test public void testGetParametersMethod() { UnitilsMethodValidator methodValidator = new UnitilsMethodValidator(new TestClass(Testclass2.class)); methodValidator.validateInstanceMethods(); Assert.assertTrue(methodValidator.getErrors() != null && !methodValidator.getErrors().isEmpty()); Assert.assertEquals("No runnable methods", methodValidator.getErrors().get(0).getMessage()); }
@SuppressWarnings("unchecked") @Override public void validateInstanceMethods() { validateTestMethods(After.class, false); validateTestMethods(Before.class, false); validateTestMethods(Test.class, false); TestClass testClass = (TestClass) Whitebox.getInternalState(this, "fTestClass", MethodValidator.class); Class<?> classUnderTest = (Class<?>) Whitebox.getInternalState(testClass, "fClass"); List<Throwable> fErrors = (List<Throwable>) Whitebox.getInternalState(this, "fErrors", MethodValidator.class); List<Method> methods = getTestMethods(testClass, classUnderTest); if (methods.size() == 0) fErrors.add(new Exception("No runnable methods")); }
private List<Method> getTestMethods(TestClass testClass, Class<?> classUnderTest) { List<Method> methods = testClass.getAnnotatedMethods(Test.class); if (methods.isEmpty()) { methods.addAll(getTestMethodsWithNoAnnotation(classUnderTest)); } return methods; }
/** * This is a rip-off of the * {@link MethodValidator#validateInstanceMethods()} with the exception that * this method also searches for test methods if the class extends * {@link TestCase} and has methods that starts with test which are not * annotated. */ @SuppressWarnings("unchecked") private void validateTestMethods(Class<? extends Annotation> annotation, boolean isStatic) { TestClass testClass = (TestClass) Whitebox.getInternalState(this, "fTestClass", MethodValidator.class); Class<?> classUnderTest = (Class<?>) Whitebox.getInternalState(testClass, "fClass"); final List<Method> methods; if (TestCase.class.equals(classUnderTest.getSuperclass()) && !isStatic) { methods = getTestMethodsWithNoAnnotation(classUnderTest); } else { methods = testClass.getAnnotatedMethods(annotation); } List<Throwable> fErrors = (List<Throwable>) Whitebox.getInternalState(this, "fErrors", MethodValidator.class); for (Method each : methods) { if (Modifier.isStatic(each.getModifiers()) != isStatic) { String state = isStatic ? "should" : "should not"; fErrors.add(new Exception("Method " + each.getName() + "() " + state + " be static")); } if (!Modifier.isPublic(each.getDeclaringClass().getModifiers())) fErrors.add(new Exception("Class " + each.getDeclaringClass().getName() + " should be public")); if (!Modifier.isPublic(each.getModifiers())) fErrors.add(new Exception("Method " + each.getName() + " should be public")); if (each.getReturnType() != Void.TYPE) fErrors.add(new Exception("Method " + each.getName() + " should be void")); if (each.getParameterTypes().length != 0) fErrors.add(new Exception("Method " + each.getName() + " should have no parameters")); } }
@Test public void testValidateArgConstructorNoParameters() throws Exception { UnitilsMethodValidator validator = new UnitilsMethodValidator(new TestClass(JustATestClass.class)); validator.validateArgConstructor(); Assert.assertFalse(validator.getErrors().isEmpty()); }
@Test public void testValidateArgConstructorWithParameters() throws Exception { UnitilsMethodValidator validator = new UnitilsMethodValidator(new TestClass(Testclass3.class)); validator.validateArgConstructor(); Assert.assertTrue(validator.getErrors().isEmpty()); }
public PowerMockJUnit4MethodValidator(TestClass testClass) { super(testClass); }
public JMHRunWith(Class<?> klass) throws InitializationError { fTestClass = new TestClass(klass); fTestMethods = getTestMethods(); validate(); }