I have an assignment for university that requires me to have all my code within a single *.java file. The problem I am currently dealing with is that I have a couple support classes written within the application so that at least some of the functionality is separated. The thing is, some of those support classes are quite simple, yet have a lot of functionality.
To make my problem clearer, here is the file structure for my main class.
public class Assignment {
// ...
protected class UtilClass1 {
// ...
}
protected class UtilClass2 {
// ...
}
// ...
}
My problem now is that I want to write JUnit test cases for these utility classes, as the various inputs I would have to test manually would be very time consuming, and what I really need is just to test the utility classes.
I have managed to write a simple test that tests a bit of functionality within this single line of code. Like so:
@Before
public void initAssignment() {
app = new Assignment();
}
@Test
public void testNumberCreate() {
assertTrue(app.new Number(new int[] {1,2,3,4,5}).toString().equals("12345"));
}
In this particular case, Number is one of the support classes I want to test. One problem I'm having is that I don't know how I would store the inner datatype in a variable, because most tests simply can not be a single line of code.
I am aware that I could solve this problem by making all the inner classes static, but I am wondering if there is another way to achieve this without making them static. Also, I would appreciate any cleaner way to test these classes as this is extremely messy.
This works for me:
public class Foo{
protected class Bar{}
public static void main(String[] args){
Foo.Bar bar = (new Foo()).new Bar();
}
}
No compilation error. You'll then be able to access any accessible method on bar
.
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.