使用对象夹具进行 JUnit 测试
大家好,我正在尝试编写一个 JUnit 测试来测试某些内容,但它只是没有点击。我应该设计一个 jUnit 测试器,它正在测试一个类,但使用另一个实现原始类作为测试装置的类。
我应该制作一个通用的圆形数组,它有一些用于某些事情的方法,例如添加到数组的前面和后面等。我不太确定我是否正确实现了数组转换,因为你不能转换一个泛型类型数组,但我们被要求使用有界通配符,所以我认为我实现它的方式是好的......这里没有注释
public class Array12<E> implements LimCapList<E>
{
private int maxSize;
private int first;
private int last;
private int size;
private E[] A12;
@SuppressWarnings("rawtypes")
public Array12(Class <? extends E> clazz, int capacity)
{
this.maxSize = capacity;
this.size = 0;
this.first = 0;
this.last = 0;
@SuppressWarnings({ "unchecked", "unused" })
Array12 A12 = new Array12(clazz, capacity);
}
现在我想要该函数做的是创建一个循环数组类型 clazz,具有尺寸容量。我正确实施了吗?我之所以问这个问题,是因为当我尝试创建 jUnit 测试器时,我遇到了困难,并且非常困惑我需要做什么才能让它运行。这是我迄今为止为 jUnit 测试器所做的...
public class LimCapListTester extends junit.framework.TestCase
{
private Array12 array12;
protected void setUP()
{
array12 = new Array12(Class<String>, 0);
}
protected void tearDown()
{
array12 = null;
}
问题是, array12 = new Array12(类, 0);
似乎工作不正常。我不确定我是否只是在 jUnit 测试器中使用了错误的语法,或者我是否错误地编写了 Array12。有关如何修复它的任何提示?
免责声明 这是一项家庭作业,所以我不是在寻找解决方案,而是在寻找一个关于我在哪里犯了编码错误的提示,也许还有一些关于如何使用不同的测试夹具编写 jUnit 测试程序的更多见解,就像我所拥有的经验一样到目前为止,我们正在为特定类编写 jUnit 测试。例如,我编写了一个实现 LinkedLists 的 List12.java,并编写了一个运行良好的 List12Tester.java。然而,在这个作业中,我需要编写一个 SomeTester.java 来测试 SomeCode.class,但使用实现 SomeCode.class 作为测试装置的 Array12。
我希望我已经尽我所能地解释了这一点,因为我真的很困惑,我确实打算向我的助教寻求帮助,但我想也许有人可以帮助我,这样当我向助教询问时我就不会显得太愚蠢了案例的答案确实很明显。 :) 谢谢大家!
Hey guys I'm trying to write a JUnit test to test something but it's just not clicking. I'm supposed to design a jUnit tester that is testing a class but uses another class that implements the original class as a test fixture.
I'm supposed to make a generic circular array that has some methods for certain things such as adding to the front of the array and back, etc. I'm not quite sure I've implemented the Array casting correctly as you can't cast a generic type array but we've been asked to use a bounded wildcard so I think the way I implemented it is okay....here it is sans comments
public class Array12<E> implements LimCapList<E>
{
private int maxSize;
private int first;
private int last;
private int size;
private E[] A12;
@SuppressWarnings("rawtypes")
public Array12(Class <? extends E> clazz, int capacity)
{
this.maxSize = capacity;
this.size = 0;
this.first = 0;
this.last = 0;
@SuppressWarnings({ "unchecked", "unused" })
Array12 A12 = new Array12(clazz, capacity);
}
Now what I want the function to do is to create a circular Array of type clazz, with size capacity. Did I implement it correctly? The reason why I ask is because when I try to create the jUnit tester, I run into a wall and am pretty stuck on what I need to do to get it going. Here is what I've got so far for the jUnit tester....
public class LimCapListTester extends junit.framework.TestCase
{
private Array12 array12;
protected void setUP()
{
array12 = new Array12(Class<String>, 0);
}
protected void tearDown()
{
array12 = null;
}
Problem is,
array12 = new Array12(Class, 0);
Doesn't seem to be working correctly. And I'm not sure if I'm just using the wrong syntax in the jUnit tester or if I wrote my Array12 incorrectly. Any hints on how to fix it?
DISCLAIMER
This is for a homework assignment so I'm not looking for a solution, but rather a hint on where I made a coding error and maybe some more insight on how to write jUnit testers with a different test fixture as all I've had experience with so far is writing a jUnit test for a specific class. For example, I wrote a List12.java that implements LinkedLists and wrote a List12Tester.java that worked fine. However, in this assignment I need to write a SomeTester.java that tests SomeCode.class but uses Array12 which implements SomeCode.class as a test fixture.
I hope I've explained it as best as I can as I'm really confused and I do plan to ask my TAs for help but I figure maybe someone could help me out so I don't look TOO stupid when asking my TA in case the answer is really obvious. :) Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
提示:检查
junit.framework.TestCase
方法的签名。当重写超类中的方法时,最好使用@Override
。如果
tearDown()
不是超类中的方法,那么编译器会抱怨。Hint: check the signatures of the methods of
junit.framework.TestCase
. When overriding a method in a super class, it's best to use@Override
If
tearDown()
wasn't a method in a super class, then the compiler would complain.