2017-04-12 29 views
0

我有一種情況如下,我不是從哪裏開始肯定,JUnit的參數傳遞

文件名應作爲參數PARAM運行jar文件

時傳遞比方說我想測試外部文件中的一組數據,並且我有一個超級類(測試套件),它們具有一號和二號

並且有兩個測試類應該擴展此類並執行測試。

我目前是JUnit的新手,所以我缺乏許多概念,需要別人的幫助。

我有類CoreManager其執行主

public static void main(String[] args) 
    { 
     if (Arrays.asList(args).contains("Import")) 
     { 
     accountInfo = new ArrayList<>(); 
     int ImportIndex = Arrays.asList(args).indexOf("Import"); 
     String fileName = args[ImportIndex+1]; 
     if (fileName.contains("xml")) 
     { 
      ParseXML parseXML = new ParseXML(); 
      accountInfo = parseXML.ParseAccounts(fileName); 

      Result result = JUnitCore.runClasses(LoginTestSuite.class); 

      for (Failure failure : result.getFailures()) { 
       System.out.println(failure.toString()); 
      } 

      System.out.println(result.wasSuccessful()); 
     } 
    } 
} 

和套房類

@RunWith(MockitoJUnitRunner.class) 

@Suite.SuiteClasses({ 
    Login.class, 
    SignUp.class 

})

public class LoginTestSuite { 
public static WebDriver driver; 
public static ArrayList<AccountInfo> Account; 
public static int SecondsToWait; 

public LoginTestSuite(WebDriver driver,ArrayList<AccountInfo> Account,int 
secondsToWait) 
{ 
this.Account = Account; 
this.SecondsToWait = secondsToWait; 
this.driver = driver; 
} 

}

而且測試類

公共類登錄{

private static WebDriver driver; 
private static ArrayList<AccountInfo> Account; 
private static int SecondsToWait; 
private static final Logger logger = Logger.getLogger(Login.class.getName()); 



@BeforeClass 
public void init(){ 
    this.driver = LoginTestSuite.driver; 
    this.Account = LoginTestSuite.Account; 
    this.SecondsToWait = LoginTestSuite.SecondsToWait; 
} 

@Before 
public void Setup(){ 

    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(SecondsToWait, 
    TimeUnit.SECONDS); 
    driver.manage().timeouts().pageLoadTimeout(SecondsToWait, 
    TimeUnit.SECONDS); 
} 

@After 
public void TearDown(){ 
    driver.quit(); 
} 



@Test 
public void TestUserLogin() throws Exception 
{ 
    // Logic 

} 
+0

您應該發佈一些代碼*說明* – 2017-04-12 12:40:18

+0

表現出一定的代碼PLZ – VedX

+0

這樣做,可以然後問具體問題 –

回答

1

你的代碼看起來混亂和包含幾個質量差的結構。最重要的是,我沒有看到測試代碼和生產代碼之間的區別。哪個是哪個?

這可能是生產代碼:

public class App { 

    public static void main(String[] args) {  
      AccountReader accountReader = new AccountReader(); 
      List<AccountInfo> accounts = accountReader.read(args); 
      // maybe do something with those accounts?       
    } 
} 

public class AccountReader { 

    private ParseXML parseXML; 

    public AccountReader() { 
     this.parseXML = new ParseXML(); 
    } 

    // extra constructor to allow dependency injection from test 
    protected AccountReader(ParseXML parseXML) { 
     this.parseXML = parseXML; 
    } 

    public List<AccountInfo> read(String[] args) {   
      return parseXML.ParseAccounts(getFileName(args));   
    } 

    private String getFileName(String[] args) { 
     List<String> arguments = Arrays.asList(args);   
     int importIndex = arguments.indexOf("Import");  
     if (importIndex < 0) { 
      throw new RuntimeException("Missing Import argument"); 
     }   
     int fileNameIndex = importIndex + 1; 
     if (fileNameIndex >= arguments.size()) { 
      throw new RuntimeException("Missing fileName argument"); 
     } 
     String fileName = args[fileNameIndex]; 
     if (!fileName.endsWith(".xml")) { 
      throw new RuntimeException("Can only import XML files"); 
     } 
     return fileName; 
    } 

} 

而且這可能是它的測試:

public AccountReaderTest { 

    private AccountReader instance; 
    @Mock // creates a mock instance which we can give desired behavior 
    private ParseXML parseXML; 
    @Mock 
    List<AccountInfo> accounts; 

    @Before 
    public void setUp() { 
     instance = new AccountReader(parseXML); 
    } 

    @Test 
    public void testHappy() {   
     // SETUP 
     String fileName = "test.xml"; 
     // specify desired behavior of mock ParseXML instance 
     when(parseXML.ParseAccounts(fileName).thenReturn(accounts); 

     // CALL 
     List<AccountInfo> result = instance.read(new String[] { "Import", fileName }); 

     // VERIFY 
     assertEquals(accounts, result); 
    } 

    @Test(expected = RuntimeException.class) 
    public void testMissingImport() {   
     instance.read(new String[] { "notImport" }); 
    } 

    @Test(expected = RuntimeException.class) 
    public void testMissingFileName() {   
     instance.read(new String[] { "Import" }); 
    } 

    @Test(expected = RuntimeException.class) 
    public void testNotXml() {   
     instance.read(new String[] { "Import", "test.properties"}); 
    } 
} 
+0

我還沒有優化過任何東西。我想要實現以下功能 1-擁有一個充當「main」的函數,以便能夠解析傳遞給java jar文件的參數發佈後012-2-參數應該包含解析並傳遞解析文件的文件測試套件 3-測試套件將運行所有與其鏈接的測試案例。 –