我有一个超类和几个子类。出于某种原因,当我运行JUnit测试时,我尝试构建子类的所有方法都失败了,当我尝试调试它时,我收到的错误消息显示
未找到源:此类文件的JAR属于JUnit 4,它不允许修改其条目上的源附件。
这是我的超级班级
public class Sequence {
protected char[] seqarr;
public boolean isValidLetter(char let){
boolean isValid = false;
if (Character.isUpperCase(let) == true || Character.isLowerCase(let) == true){
isValid = true;
}
return isValid;
}
public Sequence(char[] sarr){
boolean check = true;
for (int i = 0; i<sarr.length; i++){
if (isValidLetter(sarr[i]) == false){
check = false;
break;
}
}
if (check == true){
seqarr = sarr;
}
if (check == false){
throw new IllegalArgumentException("Invalid sequence letter for class edu.iastate.cs228.hw1.Sequence"); //Need to figure out how to make this say whatever class it came from
}
}
public int seqLength(){
return seqarr.length;
}
public char[] getSeq(){
char[] seqCopy = seqarr;
return seqCopy;
}
public String toString(){
String stringSeq = "";
for(int i = 0; i < seqarr.length; i++){
stringSeq = stringSeq + seqarr[i];
}
return stringSeq;
}
public boolean equals(Object obj){
boolean check = false;
if (obj != null && (seqarr.toString().toLowerCase() == obj.toString().toLowerCase())){
check = true;
}
return check;
}
}
这是我的一个子类
public class DNASequence extends Sequence {
@Override
public boolean isValidLetter(char let){
return "aAcCgGtT".indexOf(let) != -1;
}
public DNASequence(char[] dnaarr){
super(dnaarr);
}
}
这是该子类的测试用例 公共类DNASequenceTest {
char[] testArrOne = {'a','B','c','D'};
@Test
public void testValidLetter() {
String msg = "Method should return true if the character is a/A, c/C, g/G, or t/T";
DNASequence newDNASequence = new DNASequence(testArrOne);
assertEquals(msg, true, newDNASequence.isValidLetter('a'));
}
@Test
public void testValidLetterTwo(){
DNASequence newDNASequence = new DNASequence(testArrOne);
assertEquals(false, newDNASequence.isValidLetter('P'));
}
@Test
public void testConstructor(){
String msg = "The constructor should call the super and initialize seqarr to the input if all compnents are valid.";
DNASequence newDNASequence = new DNASequence(testArrOne);
Sequence newSequence = new Sequence(testArrOne);
assertArrayEquals(msg, testArrOne, newDNASequence.getSeq());
assertArrayEquals(msg, testArrOne, newSequence.getSeq());
}
}
编辑:我的失败名单 DNASequenceTest hw1.DNASequenceTest testValidLetterTwo(hw1.DNASequenceTest) java.lang.IllegalArgumentException:类edu.iastate.cs228.hw1.Sequence的序列号无效
at hw1.Sequence.<init>(Sequence.java:35)
at hw1.DNASequence.<init>(DNASequence.java:13)
at hw1.DNASequenceTest.testValidLetterTwo(DNASequenceTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
testValidLetter(hw1.DNASequenceTest) java.lang.IllegalArgumentException:类edu.iastate.cs228.hw1.Sequence的序列号无效
at hw1.Sequence.<init>(Sequence.java:35)
at hw1.DNASequence.<init>(DNASequence.java:13)
at hw1.DNASequenceTest.testValidLetter(DNASequenceTest.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
testConstructor(hw1.DNASequenceTest) java.lang.IllegalArgumentException:类edu.iastate.cs228.hw1.Sequence的序列号无效
at hw1.Sequence.<init>(Sequence.java:35)
at hw1.DNASequence.<init>(DNASequence.java:13)
at hw1.DNASequenceTest.testConstructor(DNASequenceTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
答案 0 :(得分:0)
问题是你总是使用testArrOne
作为DNASequence
构造函数的参数,如果字母有效,它会检查每个元素。但B
和D
不有效,因此测试始终以IllegalArgumentException
失败。
您可以创建一个仅包含有效输入的测试数组作为参数:
char[] testArrValid = { 'a', 'G', 'c', 'T' };
...
@Test
public void testValidLetter() {
String msg = "Method should return true if the character is a/A, c/C, g/G, or t/T";
DNASequence newDNASequence = new DNASequence(testArrValid); // <-- use here
assertEquals(msg, true, newDNASequence.isValidLetter('a'));
}
答案 1 :(得分:-1)
可能的原因是,您没有添加适当的类来运行junit测试
@RunWith(springjunit4classrunner.class)
class DNASequenceTest {