JUnit在类之间传递参数

时间:2017-04-12 12:37:18

标签: java junit4

我有一个如下情况,我不知道从哪里开始,

运行jar文件时,文件名应作为参数param传递

比如我想从外部文件测试一组数据,我有一个排名第一和第二的超级类(测试套件)

并且有两个测试类应该扩展此类并执行测试。

我目前刚接触JUnit,所以我缺少很多概念,需要别人的帮助。

我有类CoreManager执行主

private void setTextViewDrawableColor(TextView textView, int color) {
        for (Drawable drawable : textView.getCompoundDrawables()) {
            if (drawable != null) {
                drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
            }
        }
    }

套件类

  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 Login {

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;
}

1 个答案:

答案 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"});
    }
}