按要求对下述Java程序进行单元测试。 public class Body { public int demofunc(int n1,int n2) { int a; a = sumFunc(n1, n2); System.out.println(a); return a; } //模块A public int sumFunc(int a, int b) { int c1, c2; c1 = factorial(a); c2 = factorial(b); return (c1 + c2); }//模块B public int factorial(int n) { int rtn = 1; int i; for (i = 1; i<= n; i++) { rtn *= i; } return (rtn); }//模块C 要求: 对模块A进行单元测试时, 1.是否需要驱动模块(是/否)? 如果需要,请编写驱动模块 2.是否需要桩模块(是/否)?如果需要,请编写桩模块
时间: 2024-02-04 17:03:16 浏览: 28
1. 针对模块A进行单元测试时,需要驱动模块,因为模块A依赖于模块B和模块C的功能。下面是一个简单的驱动模块示例:
```
public class BodyTest {
private Body body;
@Before
public void setUp() {
body = new Body();
}
@Test
public void testDemoFunc() {
int n1 = 2;
int n2 = 3;
int expected = 122; //期望输出 2! + 3! = 2 * 1 + 3 * 2 * 1 = 2 + 6 = 8
int actual = body.demofunc(n1, n2);
assertEquals(expected, actual);
}
}
```
2. 针对模块A进行单元测试时,不需要桩模块,因为模块A不依赖于其他模块的接口。
相关问题
对于下列代码: class Parent { public int addValue( int a, int b) { int s; s = a+b; return s; } } class Child extends Parent { } 下述哪个方法可以加入类Child?
这是一道重复的问题,刚才我已经回答了,我再重复一遍:
由于Child类继承了Parent类,所以Child类可以使用Parent类的所有方法。因此,Child类可以直接使用Parent类的addValue方法。如果Child类需要覆盖(override)addValue方法,可以使用与Parent类相同的方法签名,如下所示:
```
class Child extends Parent {
public int addValue( int a, int b) {
int s;
s = a+b+1; // 增加了1
return s;
}
}
```
这样,Child类就拥有了一个addValue方法,它和Parent类的addValue方法有相同的方法签名,但是实现不同。
JAVA中 编译下述类的时候,会出现什么错误,如何修改为正确的代码 class AnIntergerNamedX{ int x; static public int x(){ return x; } static public void setX(int newX){ x=newX; } }
在编译上述代码时,会出现以下两个错误:
1. 静态方法访问非静态变量x
2. 非静态方法不能从静态上下文中引用
这是因为x是实例变量,而x()和setX(int newX)是静态方法,无法直接访问实例变量x。
为了修复这些错误,需要将变量x声明为static,这样它就成为了一个类变量,可以被静态方法访问。修改后的代码如下:
```
class AnIntegerNamedX {
static int x;
static public int getX() {
return x;
}
static public void setX(int newX) {
x = newX;
}
}
```
在这个修改后的代码中,变量x被声明为static,getX()和setX(int newX)方法也被声明为static,这样就可以正确地访问变量x了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)