nighteblis
nighteblis

Reputation: 93

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

Upvotes: 2

Views: 2754

Answers (2)

Tej Kiran
Tej Kiran

Reputation: 2238

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

Upvotes: 0

Owen Cao
Owen Cao

Reputation: 8183

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.

Upvotes: 2

Related Questions