[英]JUnit | How to pass parameters returned from one Junit test into another JUnit test
I have a junit test testArchive(). 我有一个junit测试testArchive()。 The Junit test tests the archive() method that archives a file and returns the url to it as a String. Junit测试测试归档文件的archive()方法,并将其作为String返回给它。 The URL is defined as an instance variable inside the Junit Test class. URL定义为Junit Test类中的实例变量。
class Prepare {
private String url = "";
@Test
public void testArchive() {
String url = getService().archive();
}
@Test
public void testSendEmail() {
getService().sendEmail(url) // Url is turning out to be null
}
} // end of class
I am writing another Junit test for sendEmail() which emails the URL. 我正在为sendEmail()编写另一个Junit测试,该测试通过电子邮件发送URL。 But the URL is turning out to be null, though its defined as a class variable 但是URL被证明是null,尽管它被定义为类变量
Can you please let me know how I need to correct my Junit test for send email? 您能告诉我如何更正我的Junit测试以发送电子邮件吗?
Thank you 谢谢
Keep in mind that: 请记住:
.archive()
looks like will produce side effects 测试用例不应该有副作用: .archive()
看起来会产生副作用 testSendEmail
seems to assume testArchive
is executed first, which is wrong 测试用例不应该假设其他测试用例的执行顺序:你的testSendEmail
似乎假设testArchive
是先执行的,这是错误的 getService
calls looks like an external (uncontrolled) factor 测试用例不应该依赖于外部因素: getService
调用看起来像一个外部(不受控制的)因素 Instead of one test cases depending on the outcome of another, you could use a private helper method that both test cases can call. 您可以使用两个测试用例都可以调用的私有帮助器方法,而不是一个测试用例取决于另一个测试用例的结果。
Short answer: 简短回答:
You should really not do that. 你真的不应该这样做。
Detailed answer: 详细解答:
Unit tests (and therefore JUnit tests as well) are intended to run separately and independently from each other. 单元测试(以及因此JUnit测试)旨在单独运行并彼此独立运行。 Each test should check only one method, regardless of result of another method or another test. 无论其他方法或其他测试的结果如何,每个测试应仅检查一种方法。 So in your case, method testSendEmail()
should use some hard coded URL, or better few different URLs. 所以在你的情况下,方法testSendEmail()
应该使用一些硬编码的URL,或者更好的几个不同的URL。
我删除了第二个JUnit测试并将测试合并为1.存档和电子邮件都将在一次测试中发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.