我需要为一个设计很差的旧应用程序编写JUnit测试,并将大量错误消息写入标准输出.当getResponse(String request)方法行为正确时,它返回XML响应:
@BeforeClass
public static void setUpClass() throws Exception {
Properties queries = loadPropertiesFile("requests.properties");
Properties responses = loadPropertiesFile("responses.properties");
instance = new ResponseGenerator(queries, responses);
}
@Test
public void testGetResponse() {
String request = "<some>request</some>";
String expResult = "<some>response</some>";
String result = instance.getResponse(request);
assertEquals(expResult, result);
}
Run Code Online (Sandbox Code Playgroud)
但是当它得到格式错误的XML或者不理解它返回的请求并将null一些东西写入标准输出时.
有没有办法在JUnit中断言控制台输出?要抓住像这样的案例:
System.out.println("match found: " + strExpr);
System.out.println("xml not well formed: " + e.getMessage());
Run Code Online (Sandbox Code Playgroud) 我必须在类中测试一个方法,该方法使用Scanner类进行输入.
package com.math.calculator;
import java.util.Scanner;
public class InputOutput {
public String getInput() {
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用JUnit测试它,但不知道如何做到这一点.
我尝试使用以下代码,但它不会工作.
package com.math.calculator;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class InputOutputTest {
@Test
public void shouldTakeUserInput() {
InputOutput inputOutput= new InputOutput();
assertEquals("add 5", inputOutput.getInput());
}
}
Run Code Online (Sandbox Code Playgroud)
我也想和Mockito一起尝试(使用模拟......当...然后返回)但不知道该怎么做.
你能解释一下如何System.in正确模拟吗?
我有一个简单的测试,我将值分配给
@Test
public void testBarCorrect() {
System.setIn(new ByteArrayInputStream("7\n2\n3".getBytes()));
someService().consume();
}
Run Code Online (Sandbox Code Playgroud)
在引擎盖下我什么都不做 new Scanner(System.in);
需要注意的是,有时我的测试是绿色的,但在 80% 时它会抛出一个 Exception "java.util.NoSuchElementException: No line found".
所以,如果我在测试中按下运行,它就会出现异常。
你能给我任何提示吗?
我的测试:
public class RecordServiceCTLIntegrationTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private static final InputStream DEFAULT_STDIN = System.in;
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setIn(DEFAULT_STDIN);
}
@After
public void rollbackChangesToStdin() {
System.setOut(originalOut);
System.setIn(DEFAULT_STDIN);
}
@Test
public void testBarCorrect() {
System.setIn(new ByteArrayInputStream("5\n1\nB\n21\n\n".getBytes()));
buildRecordServiceCTL().run();
assertEquals("Enter Room Id: No …Run Code Online (Sandbox Code Playgroud) 我试图了解如何测试用户的输入(请注意我不是在尝试模拟测试,而是测试实际用户的输入)
目前正如您在我的程序中看到的那样,我已经对我的测试用例的值进行了硬编码,并且它正在通过所有测试但是我如何获得用户的输入并进行测试.
有没有办法在我的构造函数中调用System.in并在测试类中创建MyClass1实例时传递它?
请尽可能给我一些示例代码,以便我能更好地理解.
如果我有这样的接口
public interface IMyClass{
public int getvalue1();
public int getvalue2();
public int getvalue3();
}
Run Code Online (Sandbox Code Playgroud)
然后接口实现
public class MyClass1 implements MyClass{
private int _value1 = 0;
private int _value2 = 0;
private int _value3 = 0;
public MyClass1(int number1, int number2, int number3)
{
_value1 = number1;
_value2 = number2;
_value3 = number3;
}
public void setLength1(int value1)
{
_value1 = value1;
}
public void setLength2(int length2)
{
_value2 = value2;
}
public void setLength3(int length3)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个类,它读取文件并使用扫描仪接收用户输入,如果扫描仪等于该文件中一行的一部分,它将显示来自同一行的字符串。
我将如何为此创建一个 Junit 测试方法?
这是我想要测试方法的一些代码:
Scanner Input = new Scanner(System.in);
String name = Input.nextLine();
BufferedReader br;
try{
br = new BufferedReader(new FileReader(new File(filename)));
String nextLine;
while ((nextLine = br.readLine()) != null)
{
if (nextLine.startsWith("||"))
{
int f1 = nextLine.indexOf("*");
int f2 = nextLine.indexOf("_");
fName = nextLine.substring(f1+1, f2);
if (name.equals(fname))
{
String[] s1 = nextLine.split("_");
String sName = s1[1];
System.out.println(sName);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的数据文件看起来像这样
||
*Jack_Davis
*Sophia_Harrolds
Run Code Online (Sandbox Code Playgroud)
我曾尝试在我的测试方法中使用此代码
@Test
public void testgetSurname() {
System.out.println("get surname");
String filename = "";
String expResult …Run Code Online (Sandbox Code Playgroud)