2

我的问题是关于在非公共类中测试一个方法。我不是开发人员,而是单元测试人员。

我想我知道如何在公共类中测试私有和受保护的方法。反射或编写自己的子类。

也许有什么方法可以测试非公共类中的方法?有的课是写成非公开的,能考吗?

前提条件:我无权将测试类与被测类写在同一个包中。

任何意见将不胜感激,谢谢。

代码:

package com.whoistester.a;
class a {  // non-public class

    public void methodA()
    {
    }

}

package com.whoistester.test
public class test {

    public void testa()
    {
       // here how to test the method of the class a ???
    }

}

解决方案:
我是多么愚蠢。这很容易做到。不同的项目,但 junit 测试和测试代码都可以在同一个包中。项目/src/main/com/tec/Util.java test-project/src/test/com/tec/UtilTest.java

4

2 回答 2

2

Don't want to write test class in the same package with the class tested?

Well, in my projects, I usually put the test classes in a different physical directory, but the same package as the classes they test, and this is also the structure that Maven promotes:

For example:

project/src/main/com/tec/Util.java

project/src/test/com/tec/UtilTest.java'

Organizing test code in this structure allows the test code to access package private fields. And I can easily compile and package it separately so that production jar files do not contain test code.

Hope this can help you.

于 2013-11-14T06:36:11.073 回答
0

You can not access Class (Non Public) of One Package from the other package. Non Public Class is private for other package classes.

于 2013-11-14T06:45:55.867 回答