001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.test.junit4;
017
018import com.jfinal.aop.Aop;
019import io.jboot.test.CPI;
020import org.junit.runner.notification.RunNotifier;
021import org.junit.runners.BlockJUnit4ClassRunner;
022import org.junit.runners.model.FrameworkMethod;
023import org.junit.runners.model.InitializationError;
024import org.junit.runners.model.Statement;
025
026public class JbootRunner extends BlockJUnit4ClassRunner {
027
028    public JbootRunner(Class<?> klass) throws InitializationError {
029        super(klass);
030    }
031
032    @Override
033    protected Object createTest() throws Exception {
034        Object ret = Aop.inject(super.createTest());
035        CPI.setTestInstance(ret);
036        return ret;
037    }
038
039    @Override
040    public void run(RunNotifier notifier) {
041        try {
042            CPI.startApp(getTestClass().getJavaClass());
043            super.run(notifier);
044        } finally {
045            CPI.stopApp();
046        }
047    }
048
049
050    @Override
051    protected Statement methodInvoker(FrameworkMethod method, Object test) {
052        return super.methodInvoker(method, test);
053    }
054
055
056}