junit beforeclass 每个case都执行吗

selenium+junit 多个testcase顺序执行,怎么才能让第二个testcase使用上一个已经打开的浏览器?
[问题点数:40分]
selenium+junit 多个testcase顺序执行,怎么才能让第二个testcase使用上一个已经打开的浏览器?
[问题点数:40分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。junit4 beforeClass方法每次都执行,该如何解决
&来源:读书人网&【读书人网():综合教育门户网站】
junit4 beforeClass方法每次都执行不是说beforeClass方法只会执行一次吗?怎么他跟before或after的方法一样
junit4 beforeClass方法每次都执行不是说beforeClass方法只会执行一次吗?怎么他跟before或after的方法一样,每次都执行啊?看测试代码:Java codeimport org.junit.Aimport org.junit.AfterCimport org.junit.Bimport org.junit.BeforeCimport org.junit.Tpublic class JustTest {
@BeforeClass
public static void beforeClass() {
System.out.println(&before class&);
public void before() {
System.out.println(&before&);
public void test() {
System.out.println(&test&);
public void test2() {
System.out.println(&test2&);
public void after() {
System.out.println(&after&);
@AfterClass
public static void afterClass() {
System.out.println(&after class&);
public void beforeAgain() {
System.out.println(&before again&);
}}我双击test方法 运行结果为:before classbeforebefore againtestafterafter class运行test2方法 运行结果为:before classbeforebefore againtest2afterafter class在这里,运行结果不是应该为beforebefore againtest2afterbeforeClass 和 afterClass到底怎么理解?[解决办法]beforeClass
是类加载的时候执行,before在测试方法执行前执行。你双击test方法,当这个方法运行完以后,你的类实例已经销毁了。再运行tes2() 类需要重新加载,所以BeforeClass又会继续执行Java Code Example org.junit.Before
Java Code Examples for org.junit.Before
The following are top voted examples for showing how to use
org.junit.Before. These examples are extracted from open source projects.
You can vote up the examples you like and your votes will be used in our system to product
more good examples.
+ Save this class to your library
public void setUp() {
node = nodeBuilder().local(true).node();
Client client = node.client();
indexer = new ElasticSearchIndexerImpl(client, &test&);
searcher = new ElasticSearchSearcherImpl(client, &test&);
indexer.deleteIndex();
// delete old index if exist
indexer.createIndex();
public void before() throws Exception {
client.setOptions(new ClientOptions.Builder().autoReconnect(true).build());
// needs to be increased on slow systems...perhaps...
client.setDefaultTimeout(3, TimeUnit.SECONDS);
RedisConnection&String, String& connection = client.connect();
connection.flushall();
connection.flushdb();
connection.close();
* Clears the ignored class name, package, and duplicate bean names lists
* before every test.
public void setUp() {
ignoredClassNames = new HashSet&String&();
ignoredPackages = new HashSet&String&();
ignoredDuplicateBeanNames = new HashSet&String&();
public void beforeEach() {
super.beforeEach();
setValue(0);
sensor.zero();
public void setup() {
this.fs = new MemoryFileSystem() {
public String getCurrentDirectory() {
return &/a/b&;
public long currentTimeMillis() {
return 87654;
public boolean isOwner(FakeFile fake) {
return isOwner.get();
public void setUp() {
// prepare and mock1
Map&String,NetAddress& name2AddressMapping1 = new HashMap&String,NetAddress&();
Map&NetAddress,String& address2NameMapping1 = new HashMap&NetAddress,String&();
name2AddressMapping1.put(TEST_HOSTNAME1, TEST_IP1);
address2NameMapping1.put(TEST_IP1, TEST_HOSTNAME1);
NetAddressNameService mock1Ns = new MockNetAddressNameService(name2AddressMapping1, address2NameMapping1);
// prepare and mock2
Map&String,NetAddress& name2AddressMapping2 = new HashMap&String,NetAddress&();
Map&NetAddress,String& address2NameMapping2 = new HashMap&NetAddress,String&();
name2AddressMapping2.put(TEST_HOSTNAME2, TEST_IP2);
address2NameMapping2.put(TEST_IP2, TEST_HOSTNAME2);
mock2Ns = new MockNetAddressNameService(name2AddressMapping2, address2NameMapping2);
// SwitchingNetAddressNameService on top
switchingNs = new SwitchingNetAddressNameService(mock1Ns);
// CachingNetAddressNameService on top
ns = new CachingNetAddressNameService(switchingNs,
MAX_ELEMENTS_IN_CACHE, IS_NAME_CASE_SENSITIVE,
CACHE_TTL_SECONDS, CACHE_NEGATIVE_TTL_SECONDS);
public void setUp() throws Exception {
reposRoot = new File(getResource(PATH), &repos&);
if (!reposRoot.mkdirs() && !reposRoot.isDirectory()) {
throw new IllegalStateException(&Cannot create reposRoot&);
repoA = new File(reposRoot, &root-a&);
if (!repoA.mkdirs() && !repoA.isDirectory()) {
throw new IllegalStateException(&Cannot create reposA&);
repoB = new File(reposRoot, &root-b&);
if (!repoB.mkdirs() && !repoB.isDirectory()) {
throw new IllegalStateException(&Cannot create reposB&);
public void setup() {
done = new LinkedList&String&();
failed = new LinkedList&Exception&();
deferred = new Deferred&Integer&();
promise = deferred.
promise.then(Callbacks.&Integer&toStringHandler(), Callbacks.&Exception&toStringHandler()).then(Callbacks.addToHandler(done), Callbacks.addToHandler(failed));
public void setUp() {
MockitoAnnotations.initMocks(this);
List&WebElement& divs = new ArrayList&WebElement&();
for (int i = 1; i &= 6; i++) {
WebElement elem = mock(WebElement.class);
when(elem.getText()).thenReturn(&& + i);
switch(i) {
case 1 : when(elem.getAttribute(&class&)).thenReturn(&odd first&);
case 2 : when(elem.getAttribute(&class&)).thenReturn(&even second&);
case 3 : when(elem.getAttribute(&class&)).thenReturn(&odd&);
case 4 : when(elem.getAttribute(&class&)).thenReturn(&even&);
case 5 : when(elem.getAttribute(&class&)).thenReturn(&odd&);
case 6 : when(elem.getAttribute(&class&)).thenReturn(&even last&);
divs.add(elem);
when(myFragment.getDivs()).thenReturn(divs);
Example 10
@Before public void setup() {
path = ConfigTest.getCleanDir() ;
location = Location.create (path) ;
if ( useTransactionsSetup )
setupTxn() ;
setupPlain() ;
Example 11
public void setup() {
MockitoAnnotations.initMocks(this);
Mockito.when(this.brokerChannel.send(Matchers.any(WampMessage.class)))
.thenReturn(true);
Mockito.when(this.clientOutboundChannel.send(Matchers.any(WampMessage.class)))
.thenReturn(true);
this.eventMessenger = new EventMessenger(this.brokerChannel,
this.clientOutboundChannel);
Example 12
public void setUp() throws Exception {
super.setUp();
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, &Test Username.&), TestConstants.TEST_USER_NAME);
assertNotNullOrEmpty(String.format(RESOURCE_MISSING_MESSAGE, &Test API Key.&), TestConstants.TEST_API_KEY);
authentication = new LoginTokenAuthentication(TestConstants.TEST_USER_NAME, TestConstants.TEST_API_KEY);
factory = GitHubServiceFactory.newInstance();
Example 13
public void setUp() throws Exception {
Random r = new Random(1);
plain = new long[numentries];
array = new SequenceInt64(numentries);
for(int k=0;k&k++) {
long value = Math.abs(r.nextLong());
array.append(value);
Example 14
* Construction of streams and engine useful for tests.
public void setup() {
streamRule1 = new StringStream(&att1,att2\nval1,\n,val2\n,&);
rule1Engine =
new CsvEngine(EngineParameters.createBuilder().setValidation(true).setNbLinesWithErrorsToStop(999).build(),
ObjectRule1_1.class, ObjectRule1_2.class, ObjectRule1_3.class, ObjectRule1_4.class);
Example 15
* @throws java.lang.Exception
public void setUp() throws Exception {
UscConfigurationServiceImpl.setDefaultPropertyFilePath(&src/test/resources/etc/usc/usc.properties&);
/*Logger root = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
if (root instanceof SimpleLogger) {
SimpleLogger.setLevel(SimpleLogger.TRACE);
// set up client bootstrap
clientBootstrap.group(localGroup);
clientBootstrap.channel(LocalChannel.class);
clientBootstrap.handler(new ChannelInitializer&LocalChannel&() {
public void initChannel(LocalChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new LoggingHandler(&UscPluginTest client&, LogLevel.TRACE));
// Decoders
p.addLast(&frameDecoder&, new DelimiterBasedFrameDecoder(80, false, Delimiters.lineDelimiter()));
p.addLast(&stringDecoder&, new StringDecoder(CharsetUtil.UTF_8));
// Encoder
p.addLast(&stringEncoder&, new StringEncoder(CharsetUtil.UTF_8));
Example 16
public void setUp() throws Exception {
final Hashtable&String, String& environment = new Hashtable&String, String&();
environment.put(Context.INITIAL_CONTEXT_FACTORY, &com.btmatthews.mockjndi.core.MockInitialContextFactory&);
environment.put(&com.btmatthews.mockjndi.config&, &classpath:mockjndi.xml&);
rootContext = new InitialContext(environment);
Example 17
public void setUp() {
nominalLabel = new NominalLabelDTO(nominalAttribute, &test_nominal_label_code&, &test_nominal_label_name&, &test_nominal_label_desc&, 500);
} catch (PropertyNotSetException e) {
logger.error(&PropertyNotSetException while creating object of nominal label &+e.getMessage());
entityManager.getTransaction().begin();
nominalLabel = nominalLabelResourceFacadeImp.addNominalLabel(nominalLabel);
entityManager.getTransaction().commit();
Example 18
public void setUp() throws Exception {
demArray = new float[] {//
ND, ND, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, 99, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 95, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 90, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 85, ND, ND, ND, ND, ND, ND, ND,//
ND, 80, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 75, 70, 65, 60, ND, ND, 45, ND,//
ND, ND, ND, ND, ND, ND, 55, 50, ND, ND,//
ND, ND, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, ND, ND, ND, ND, ND, ND, ND, ND,//
RasterMetadata rasterMetadata = new RasterMetadata(0, 15, 1, -1, ncols,
dem = GeoRasterFactory.createGeoRaster(demArray, rasterMetadata);
dem.setNodataValue(ND);
directionArray = new float[] {//
ND, ND, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, 8, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 7, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 7, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 6, ND, ND, ND, ND, ND, ND, ND,//
ND, 8, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 1, 1, 1, 8, ND, ND, -1, ND,//
ND, ND, ND, ND, ND, ND, 1, 2, ND, ND, //
ND, ND, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, ND, ND, ND, ND, ND, ND, ND, ND,//
direction = dem.doOperation(new D8OpDirection());
accumulation = direction.doOperation(new D8OpAccumulation());
accumulationArray = new float[] {//
ND, ND, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, 1, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 2, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 3, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 4, ND, ND, ND, ND, ND, ND, ND,//
ND, 5, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, 6, 7, 8, 9, ND, ND, 12, ND,//
ND, ND, ND, ND, ND, ND, 10, 11, ND, ND, //
ND, ND, ND, ND, ND, ND, ND, ND, ND, ND,//
ND, ND, ND, ND, ND, ND, ND, ND, ND, ND,//
Example 19
@SuppressWarnings({&unchecked&, &rawtypes&})
public void setup() {
actionConfigurer = ActionConfigurerFactory.createCustomConfigurer(
properties, messageSource, conversionService,
Arrays.asList(actionProperty));
when(actionProperty.getMessageCode(IDENTIFIER))
.thenReturn(MESSAGE_CODE);
when(actionProperty.getTargetType()).thenReturn((Class) TARGET_TYPE);
when(actionProperty.getSwingKey()).thenReturn(SWING_KEY);
Example 20
public void before() throws Exception {
fac = XMLSignatureFactory.getInstance(&DOM&, CSPXML_PROVIDER);
compareClasses(&XMLSignatureFactory&, fac.getClass(), pany.security.csp.xml.dsig.internal.dom.DOMXMLSignatureFactory.class);
signKeys = new ArrayList&Key&();
KeyStore keyStore = java.security.KeyStore.getInstance(STORE_NAME, CSP_PROVIDER);
keyStore.load(null, null);
for(Enumeration&String& aliases = keyStore.aliases(); aliases.hasMoreElements(); ) {
String alias = aliases.nextElement();
Key key = keyStore.getKey(alias, null);
if(key != null) {
compareClasses(&PrivateKey&, key.getClass(), pany.security.csp.CSPPrivateKey.class);
signKeys.add(key);Junit使用入门培训(初级篇)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
Junit使用入门培训(初级篇)
上传于||文档简介
&&J​u​n​i​t​使​用​入​门​培​训​(​初​级​篇​)
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
下载文档到电脑,查找使用更方便
还剩17页未读,继续阅读
你可能喜欢1465人阅读
JUnit(3)
在JUnit 4.10中,除了@Before、@After这些标注可以在case运行前后分别执行之外,还有一种方式对于监控case运行状态更为精确——TestWatcher,看下面的例子:
package cskgnt.
import org.junit.A
import org.junit.AfterC
import org.junit.B
import org.junit.BeforeC
import org.junit.R
import org.junit.rules.TestN
import org.junit.rules.TestW
import org.junit.runner.D
public abstract class Father {
protected String caseIdentifier = &&;
protected abstract void sonAfter();
public TestName name = new TestName();
@BeforeClass
public static void fatherBeforeClass() {
System.out.println(&Father: beforeClass&);
@AfterClass
public static void fatherAfterClass() {
System.out.println(&Father: afterClass&);
public void fatherBefore() {
System.out.println(&Father: before&);
public void fatherAfter() {
System.out.println(&Father: after&);
public TestWatcher watchman = new TestWatcher() {
protected void starting(Description d) {
caseIdentifier = d.getClassName() + &.& + d.getMethodName();
System.out.println(&starting: & + caseIdentifier);
protected void succeeded(Description d) {
caseIdentifier = d.getClassName() + & & + d.getMethodName();
System.out.println(&succeeded: & + caseIdentifier);
protected void failed(Throwable e, Description d) {
caseIdentifier = d.getClassName() + & & + d.getMethodName();
System.out.println(&failed: & + caseIdentifier);
protected void finished(Description d) {
sonAfter();
caseIdentifier = d.getClassName() + & & + d.getMethodName();
System.out.println(&finished: & + caseIdentifier);
package cskgnt.
import org.junit.AfterC
import org.junit.B
import org.junit.BeforeC
import org.junit.T
public class Son extends Father {
public void testCase() {
System.out.println(&TestCase: & + name.getMethodName());
public void extendBefore() {
System.out.println(&Son: before&);
public void sonAfter() {
System.out.println(&Son: after&);
@BeforeClass
public static void extendBeforeClass() {
System.out.println(&Son: beforeClass&);
@AfterClass
public static void extendAfterClass() {
System.out.println(&Son: afterClass&);
子类的testCase的执行结果如下:
Father: beforeClass
Son: beforeClass
starting: Son.testCase
Father: before
Son: before
TestCase: testCase
Father: after
succeeded: com.taobao.ashu.test.Son testCase
Son: after
finished: com.taobao.ashu.test.Son testCase
Son: afterClass
Father: afterClass 说明如下:
1、Father类中定义了TestName的一个对象name,用于在case中获取当前case名称;
2、Father类中定义的TestWatcher的一个对象watchman,用于监控case运行状态。当前重载了TestWatcher的四个方法:starting、succeeded、failed、finished,它们的执行顺序为:starting-&succeeded/failed(二选一)-&finished,starting、finished一定会执行,succeeded、failed视case运行情况会执行二选一
3、Father类声明了一个抽象方法sonAfter希望在子类实现,这个方法会在finished时调用
4、之所以没为子类的sonAfter加上@After标注,是希望在@After之前,根据case执行是否成功来决定是否做些其它工作(如假设case失败,则保存一些日志、数据等,如果case成功则打印成功日志),如果不这样做,通过运行结果可以看到,case无论结果是否成功,都会先进入@After标注的sonAfter,在这里是没法看到case运行结果的,是否保存日志也就不能判断。现在的方式是直接放弃@After标注,而使用finished调用自己实现的sonAfter
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:94479次
积分:1002
积分:1002
排名:千里之外
原创:14篇
转载:14篇
(1)(1)(1)(1)(3)(5)(2)(3)(2)(1)(1)(2)(3)(2)}

我要回帖

更多关于 org.junit.before jar 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信