使用对象夹具进行 JUnit 测试

发布于 2024-10-16 21:56:30 字数 1604 浏览 3 评论 0原文

大家好,我正在尝试编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇年纸鸢 2024-10-23 21:56:30

提示:检查junit.framework.TestCase方法的签名。当重写超类中的方法时,最好使用@Override

@Override
protected void tearDown() {
  array12 = null;
}

如果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

@Override
protected void tearDown() {
  array12 = null;
}

If tearDown() wasn't a method in a super class, then the compiler would complain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文