更新:
我有一个像这样的代码:
class Sample
{
public boolean isALive(boolean isLive, int num)
{
return isLive ? num == 3 : num == 2 || num == 3;
}
}
在测试中,它将是这样的,
具有各种组合的 assertTrue(x.isAlive(true,1);
如果不使用三元运算符,我想以更简单的方式返回相同的结果。
有办法吗?
解决方案: return num == 3 || (isLive&& num == 2); // - 解决方案。
答案 0 :(得分:0)
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.runner.RunWith;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;
@RunWith(ZohhakRunner.class)
public class SampleTest {
Sample sample = new Sample();
@TestWith({
"true, 3, true",
"true, 2, false",
"false, 3, true",
"false, 2, true",
"false, 1, false"
})
public void should_xxx(boolean isLive, int num, boolean expectedResult) {
boolean actualResult = sample.isALive(isLive, num);
assertThat(actualResult).isEqualTo(expectedResult);
}
}
然后我会写:
import static org.apache.commons.lang3.ArrayUtils.contains;
public class Sample {
public boolean isALive(boolean isLive, int num) {
int[] liveValues = {3};
int[] deadValues = {2, 3};
int[] validValues = isLive ? liveValues : deadValues;
return contains(validValues, num);
}
}