简体   繁体   English

如何在Eclipse中创建预格式化的Java文件?

[英]How do I create pre-formatted Java files in Eclipse?

I am currently writing JUnit test cases using the Selenium-RC API. 我目前正在使用Selenium-RC API编写JUnit测试用例。 I am writing my scripts like: 我正在写我的脚本,例如:

//example JUnit test case

public class myScripts extends SeleneseTestCase {

   public void setUp() throws Exception {
      SeleniumServer newSelServ = new SeleniumServer();
      newSelServ.start();
      setUp("https://mySite.com", "*firefox");
   }

   public void insert_Test_Name throws Exception {

       //write test code here

   }
}

And for each test, I have a new JUnit file. 对于每个测试,我都有一个新的JUnit文件。 Now, since the beginning of my JUnit files will all basically be the same, just with minor variations towards the end, I was thinking about creating a pre-formatted Java template to write create a file with the redundant code already written. 现在,由于我的JUnit文件的开头基本上都相同,只是在结尾处稍有变化,所以我正在考虑创建一个预格式化的Java模板,以使用已经编写的冗余代码编写文件。 However, I can't find any information on whether this is possible. 但是,我找不到有关是否可行的任何信息。 Does Eclipse allow you to create file templates for certain packages? Eclipse是否允许您为某些软件包创建文件模板?

Create a super class to add all the common code. 创建一个超类以添加所有通用代码。 Creating template is really bad because of the fact you are duplicating the code at the end of the day. 创建模板真的很糟糕,因为事实上您要在一天结束时复制代码。

class Super extends SeleneseTestCase{
// Add all common code
}

class Test1 extends Super{
   // only special test case logic

}

Also I would suggest not to create SeleniumServer instance for each test case, It will reduce overall performance of the test suite. 我也建议不要为每个测试用例创建SeleniumServer实例,这会降低测试套件的整体性能。 You can reuse object as long as you are running test sequentially. 只要按顺序运行测试,就可以重用对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM