gpt4 book ai didi

java - JUnit 如何知道要测试哪个类?

转载 作者:行者123 更新时间:2023-11-29 05:51:17 25 4
gpt4 key购买 nike

我有两个类 Calculator1Calculator2,它们位于 src 文件夹下名为 com.zzy.junit.user 的包中。我用myeclipse创建了两个测试类分别进行测试。当我执行其中的一个测试类时,Junit4如何知道测试的是哪个类?

1 类

package com.zzy.junit.user;

public class Calculator1 {

private int a;
private int b;

public Calculator1(int a,int b)
{
this.a = a;
this.b = b;
}

public int add()
{
return a+b;
}
}

第 2 类

package com.zzy.junit.user;

public class Calculator2{

public int divide(int a,int b)
{
return a/b;
}
}

还有两个测试类如下:它们都在名为 test 的 src 文件夹中

测试类 1

package com.zzy.junit.user;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

import org.junit.Test;

public class TestCalculator1 {

@Test
public void testAdd() {
Calculator1 s = new Calculator1(3,6);
int z = s.add();
assertThat(z,is(9));
}

}

测试类 2

package com.zzy.junit.user;

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;

import org.junit.Test;

public class TestCalculator2 {

@Test
public void testDivide() {
TestCalculator2 t = new TestCalculator2();
int z = t.divide(4, 2);
assertThat(z,is(2));
}

}

我想知道如果我执行名为TestCalculator2 的测试类,Junit4 如何知道我确实要测试Calculator1 类。与测试类名称有关吗?

最佳答案

JUnit 是一个用于执行可以通过或失败的测试的框架。事实上,单个测试通常关注单个类,这是大多数 Java 程序员遵守的惯例。 JUnit4 不知道正在测试哪些类,它只是执行所有用 @Test 注释的方法。

关于java - JUnit 如何知道要测试哪个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13774284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com