/** * Returns a {@link Statement}: if {@code method}'s {@code @Test} annotation * has the {@code expecting} attribute, return normally only if {@code next} * throws an exception of the correct type, and throw an exception * otherwise. */ protected Statement possiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next) { Test annotation = method.getAnnotation(Test.class); return expectsException(annotation) ? new ExpectException(next, getExpectedException(annotation)) : next; }
protected Statement possiblyExpectingAccessException(final Statement next, final boolean failWithAccessException) { if (failWithAccessException) { return new ExpectException(next, EJBAccessException.class); } else { return next; } }
@Test public void whenTestAnnotationDoesSpecifyAnException_returnExpectExceptionStatement() throws Exception { TestClass testClass = TestClassPool.forClass(SingleTestWithExpectedAnnotation.class); FrameworkMethod method = testClass.getAnnotatedMethods(Test.class).get(0); Statement actual = builder.createStatement(testClass, method, target, next, description, notifier); assertThat(actual, is(instanceOf(ExpectException.class))); }
@Override protected Statement methodInvoker(FrameworkMethod method, Object test) { boolean isNew = false; boolean inject = false; if (method instanceof DbFrameworkMethod) { DbFrameworkMethod dbCase = (DbFrameworkMethod) method; String dbType = dbCase.getDbType(); Object obj = connections.get(dbType); if (obj == null) { throw new IllegalArgumentException("Database " + dbType + " is unknown."); } DbConnectionHolder holder; if (obj instanceof DataSource) { holder = createDbClient((DataSource) obj);// CreateDbClient isNew = true; } else { holder = (DbConnectionHolder) obj; if (holder.db == null) { holder = createDbClient(holder.datasource);// CreateDbClient } } inject(test, holder.db, holder.datasource.field()); inject=true; } else if (isRouting) { if (routingDbClient == null) { MapDataSourceLookup lookup = new MapDataSourceLookup(); for (Entry<String, Object> entry : connections.entrySet()) { if (entry.getValue() instanceof DataSource) { DataSource ds = (DataSource) entry.getValue(); lookup.put(ds.name(), new SimpleDataSource(ds.url(),ds.user(),ds.password())); } } RoutingDataSource rds = new RoutingDataSource(lookup); routingDbClient = new DbClient(rds); isNew = true; } inject(test, routingDbClient, ""); inject=true; } if (isNew) { List<FrameworkMethod> methods = getTestClass().getAnnotatedMethods(DatabaseInit.class); try { for (FrameworkMethod m : methods) { printMethod(m, method); m.getMethod().invoke(test); } } catch (Exception e) { throw DbUtils.toRuntimeException(e); } } printMethod(method, null); if(inject){ return super.methodInvoker(method, test); }else{ System.err.println("数据库未配置,跳过测试:"+super.describeChild(method).getDisplayName()); return new ExpectException(null, NullPointerException.class); } }
private VerifierStatement(Statement base) { this.base = base; this.expectsException = ExpectException.class.isAssignableFrom(base.getClass()); }
public Statement createStatement(final TestClass testClass, final FrameworkMethod method, final Object target, final Statement next, final Description description, final RunNotifier notifier) { final Test annotation = method.getAnnotation(Test.class); return annotation.expected() == Test.None.class ? next : new ExpectException(next, annotation.expected()); }
/** * Returns a {@link org.junit.runners.model.Statement}: if {@code method}'s {@code @Test} annotation * has the {@code expecting} attribute, return normally only if {@code next} * throws an exception of the correct type, and throw an exception * otherwise. * * @deprecated Will be private soon: use Rules instead */ @Deprecated protected Statement possiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next) { Test annotation = method.getAnnotation(Test.class); return expectsException(annotation) ? new ExpectException(next, getExpectedException(annotation)) : next; }
/** * Returns a {@link Statement}: if {@code method}'s {@code @Test} annotation * has the {@code expecting} attribute, return normally only if {@code next} * throws an exception of the correct type, and throw an exception * otherwise. * * @deprecated Will be private soon: use Rules instead */ @Deprecated protected Statement possiblyExpectingExceptions(FrameworkMethod method, Object test, Statement next) { Test annotation = method.getAnnotation(Test.class); return expectsException(annotation) ? new ExpectException(next, getExpectedException(annotation)) : next; }
/** * Perform the same logic as * {@link BlockJUnit4ClassRunner#possiblyExpectingExceptions(FrameworkMethod, Object, Statement)} * except that the <em>expected exception</em> is retrieved using * {@link #getExpectedException(FrameworkMethod)}. */ @Override protected Statement possiblyExpectingExceptions(FrameworkMethod frameworkMethod, Object testInstance, Statement next) { Class<? extends Throwable> expectedException = getExpectedException(frameworkMethod); return (expectedException != null ? new ExpectException(next, expectedException) : next); }
/** * Performs the same logic as * {@link BlockJUnit4ClassRunner#possiblyExpectingExceptions(FrameworkMethod, Object, Statement)} * except that the <em>expected exception</em> is retrieved using * {@link #getExpectedException(FrameworkMethod)}. */ @Override protected Statement possiblyExpectingExceptions(FrameworkMethod frameworkMethod, Object testInstance, Statement next) { Class<? extends Throwable> expectedException = getExpectedException(frameworkMethod); return expectedException != null ? new ExpectException(next, expectedException) : next; }