gpt4 book ai didi

java - JUnit 在类之间传递参数

转载 作者:行者123 更新时间:2023-12-02 13:26:01 27 4
gpt4 key购买 nike

我有一个如下场景,我不确定从哪里开始,

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

例如,我想测试来自外部文件的一组数据,并且我有一个父类(super class)(测试套件),其中有第一和第二

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

我目前是 JUnit 的新手,所以我缺乏很多概念,需要有人的帮助。

我有执行 main 的 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;
}

}

和测试类

公共(public)类(class)登录{

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

}

最佳答案

您的代码看起来很困惑,并且包含一些质量较差的结构。最重要的是,我没有看到测试代码和生产代码之间的区别。哪个是哪个?

这可能是生产代码:

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

关于java - JUnit 在类之间传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43369759/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com