I have a spring application and I want to create a unitary test on a controller like this one. The problem is that the Wrapper class is a private inner class, so Wrapper is not understood in the test. Is it possible to mock it with Mockito without changing the controller class. I can use prepareData() to get an instance of the object, but I don't know if this could be used to mock that object.
Thanks
@Controller
public class Controller {
private class Wrapper {
private Object1 field1;
private Object2 field2;
private Object1 method1(){
...
}
private Object2 method1(){
...
}
}
@ModelAttribute("data")
public Wrapper prepareData() {
return new Wrapper ();
}
public String save(@ModelAttribute("data") Wrapper wrapper, BindingResult result, Model model){
...
}
}
So in my test I would have something like this
@Test
public void usernameEmpty(){
BindingResult result = Mockito.mock(BindingResult.class);
Model model = Mockito.mock(Model.class);
Wrapper data = //how to mock it
when(data.method1()).then(new Foo1());
when(data.method2()).then(new Foo2());
String returned = controller.save(data, result, model);
....
}
Hi, I would strongly advice against mocking private inner classes since usually there are other more elegant ways of testing your problem and the test case can end up being very complex. Personally I would try to refactor the code if the test gets too complex or write an integration test instead of mocking at all.
Accessing the Private Members To instantiate the inner class, initially you have to instantiate the outer class. Thereafter, using the object of the outer class, following is the way in which you can instantiate the inner class. Outer_Demo outer = new Outer_Demo(); Outer_Demo. Inner_Demo inner = outer.
Yes, you can instantiate a private inner class with Java reflection. To do that, you need to have an instance of outer class and invoke the inner class constructor which will use outer class instance in its first argument. @popgalop Inner classes are the same as methods.
Inner classes can access the variables of the outer class, including the private instance variables. Unlike the non-static nested classes, the static nested class cannot directly access the instance variables or methods of the outer class. They can access them by referring to an object of a class.
Your test is on methods, but it tests the whole class behavior. If your inner class is private then its an implementation detail. Something that the test shouldn't know. It there's a lot of behaviour in that inner-class and you want to test it independently maybe you should make it public and separate from this class.
Maybe you think: but then... it's a lot of code to test (a very big indivisible thing), can't I test something smaller? Well... yes. Test Driven Development mandates to make a minimal implementation and add more code only if you add more tests. So you start with some test and minimal implementation and evolve both of them until the tests have all the specification and the code all the implementation.
So don't worry about private inner classes. Test your class contract!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With