简体   繁体   中英

How to test a method in non-public class ? I think it's NOT the same to test the private or protected method in a public class

My question is about test a method in non-public class. I'm not the developer, but a unit tester.

I think I know how to test a private and protected method in a public class. reflection or write your own subclass.

Maybe is there any way to test a method in a non-public class? Some class is written to be non-pubilic, is able to test it?

Pre-condition: I don't have rights to write test class in the same package with the class tested.

Any comments will be appreciated, thanks.

Code:

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 ???
    }

}

Solutions:
How dumb am I. It's easy to do this. Different projects but junit test and tested code both could be in the same package. project/src/main/com/tec/Util.java test-project/src/test/com/tec/UtilTest.java

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM