我正在尝试使用 jUnit 测试每个 switch/case block ,但我不确定如何让每个 TestCase 对应于不同的 switch/case。
我目前在 MenuTest.java 中有以下代码:
package com.cw.hospital.test;
import org.junit.Test;
import org.junit.Assert;
import com.cw.hospital.Menu;
public class MenuTest {
Menu MainMenu;
public MenuTest() {
super();
MainMenu = new Menu();
}
@Test
public void Case1() {
}
@Test
public void Case2() {
}
@Test
public void Case3() {
}
}
我的菜单类(正在测试的类)有一个 main() 方法。在该方法中,我正在读取用户输入,然后切换该输入。
如何将不同的输入从测试类发送到菜单?
最佳答案
如果将 I/O 内容和输入消耗等内容分开,测试会更容易。我的意思是,将 I/O 内容放在某处(可能在您的 main 中),然后创建一个将输入作为参数(可能是字符串)的方法。
然后只需使用强制 switch 语句中的路径的参数值从 JUnit 测试中调用消耗方法即可。例如:
[in the class under test]
void methodName()
{
String theInput;
... perform I/O and populate "theInput"
consume(theInput);
}
void consume(final String input)
{
switch (someting based on input)
{
...
}
}
[in your junit]
@Test
testConditionOne()
{
consume("force condition one");
}
@Test
testConditionTwo()
{
consume("force condition two");
}
... etc.
关于java - 使用 jUnit 测试每个开关/外壳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8330741/