How to create a dummy instance unsing jmockit? - java

For a test I like to create a new instance of ComplicatedClass . In reality it's very complicated to crate this instance, but I don't need the real constructor to run nor any of it's data. All I need is an object of ComplicatedClass. How can I do that?
public class ComplicatedClass {
public ComplicatedClass(/* lots of dependencies */) {
}
}
#Test
public class SomeTest {
public void test1() {
ComplicatedClass complicatedInstance = /* new ComplicatedClass(); /*
AnotherClass ac = new AnotherClass(complicatedInstance);
/* ... */
}
}

#Tested annotation does this:
#Tested ComplicatedClass complicatedInstance;
That's it. Please note that the above won't do any mocking. It is just convenient way of creating instances without calling consturctors, etc.
If you want ComplicatedClass to be mocked, use #Mocked annotation:
#Mocked ComplicatedClass complicatedInstance;
In this case, you also get your instance automatically created, but the instance is mocked.

#Tested internally instantiates the class object.
But in case of Junit test case writing of singleton class how #Tested internally creates instance because for singleton private constructor is there.

Related

Unit testing a method of a class that has a constructor and an autowired field that needs to be mocked using mockito

I have a service class that extends another service with a constructor. This class has an autowired field and a method that I wanted to unit test using Mockito. However, I am having trouble writing a unit for it.
Let say the service looks somewhat like this:
#Service
public class SomeService extends Service {
#Autowired
private SomeClient someClient;
public SomeService(Product product, #Qualifier(Constants.SOME_BEAN) Details temp) {
super(product, temp);
}
#Override
public State create() {
Request request = new Request();
request.set...
request.set..
Status status = someClient.createTenant(request);
..
.. // construct a State object and return
}
}
Now, I am writing a test for this class and I am trying to unit test the method create() above. I am having trouble mocking someClient there when this method is called by my test.
The test class looks somewhat like:
#RunWith(MockitoJUnitRunner.class)
public class SomeServiceTest {
private Detail temp;
private SomeFacade service;
private SomeClient someClient;
private Product product;
#Before
public void setup() {
temp = mock(Details.class);
product = mock(Product.class);
service = spy(new SomeService(product, temp));
someClient = mock(SomeClient.class);
}
#Test
public void test() {
Status status = new Status();
status.setStatus(...);
when(someClient.createTenant(any())).thenReturn(status);
State state = service.create();
// asserts
}
}
What I want to is that when I call service.create in the test above, the call
someClient.createTenant(request); in the method tested should be mocked to return a value and this is something I am not able to get working. Any help?
We can use Mockito's #InjectMocks-annotation to inject mocks into our unit under test. For this, we also need to
remove mock intialization from the setup()-method and
annotate the mocks with #Mock
Remarks:
Instead of field injection, I would recommend constructor injection. This allows, among other things, to keep the structure of the code and create the unit under test within setup() by manual injection
I would also recommend to add a type to the when: when(someClient.createTennant(any(Request.class)))...
As was pointed out by #second, the spy on the unit under test is superfluous.
Also pointed out by #second was the fact that field injection will not work if a constructor is present

unit testing a method called from another class

I am new to Android unit testing and I am using Mockito to do it.
I want to test my method which has a method from another class. I want to stub that method so that it should not be called. I am using doReturn().when() so that original method is not called but it is calling the original method.
Here is my code:
doReturn(true).when(myclass1mock).methodofclass1();
boolean a = myclass1mock.methodofclass1(); //here it return true
class2spy.methodofclass2(anyvalue);
The method I am testing is:
public class2 {
public void methodofclass2(Value) {
boolean value = class1.methodofclass1(); //here I don't want to call this method
}
}
The problem is method of class1 is called everytime. I want something so that class1.methodofclass1() is not called.
I am injecting using:
#Mock
class1 myclass1mock;
#InjectMocks
class2 myclass2;
#Before
public void setUp() {
myclass2 = new myclass2();
class2spy = Mockito.spy(myclass2);
}
As you want to test the behavior of Class2, then i think you mixed up the annotations. Also i would take advantage of #Spy annotations rather that configuring it by hand:
#Spy
class1 myclass1Spy;
#InjectMocks
class2 myclass2;
#Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
Also, do not try not to spy the class which is being under test (class2). Use the real implementation.

Mock class without PowerMock in arquillian Test

I would like to now, if it is possible to mock a class like
public class MyClass{
...
}
Our business logic create this object with new myClass() somewhere in the code and therefore I don't have access to the created object to mock those methods. Is there a way to replace the whole class or to overwrite those methods. I'm using mockito and I only found examples to do this like
#Test
public void myTest{
MyClass myClass = Mockito.mock(MyClass.class);
Mockito.when(myClass.myMethod()).thenReturn("hello World");
...
}
We can't use PowerMock because it isn't compatible with our test environment.
Any suggestions are welcome.
Sorry, i see only two hardcore solutions:
Make Powermock working by using PowerMockRule and PowerMock Rule Agent:
Getting javassist not found with PowerMock and PowerRule in Junit with Mockito
or
Use Java Reflection to set the mocked object to MyClass: Is it possible in Java to access private fields via reflection
I found a passable way to achieve my requirement.
I converted MyClass to a bean, just add annotation #Dependent.
#Dependent
public class MyClass{
public Integer someMethod(){
return 100;
}
}
I use MyClass in business logic like that
public class BusinessClass{
#Inject
MyClass myClass
public Integer doSomething(){
return myClass.someMethod();
}
}
After this change I'm able to mock the class in my test with annotation #Specializes
#Specializes
#Dependent
public class MyClassMock extends MyClass
{
#Override
public Integer someMethod(){
return 23; // return my mocked value
}
}
MyClass will be automatically replaced with MyClassMock in testenvironment.

How to call a non static public method from a Singleton class which is using Google #Inject

I have a Singleton class that I want to test. It uses a #Inject annotation for that class's contructor. Now for testing I want to call a public method for that class in my test class but unable to do so. I have mocked an object that is getting passed to the constructor.
#Inject
private SomeClass(SomeOtherClassObject obj) {
super(obj);
}
I mocked the above private constructor in the following way:
Singleton mockSingleton = PowerMock.createMock(Singleton.class);
PowerMock.expectNew(Singleton.class).andReturn(mockSingleton);
I dont understand how do I call the following method
public SomeClass someMethod(int 1, String 2){
//some logic
return (Object of SomeClass)
}
Any help will be appreciated. Thank You.
If you are using guice as well, you can provide a module in your test that binds SomeOtherClassObject to your mock instance. Then create the SomeClass instance via Guice's injector.
#Test
public void test() {
SomeOtherClassObject other = ...; // what ever you need to create the Mock
Injector injector = Guice.createInjector(new AbstractModule(){
public void configure() {
bind(SomeOtherClassObject.class)toInstance(other);
}
});
SomeClass some = injector.getInstance(SomeClass.class); // guice takes care of the constructor injection
some.someMethod(...);
}
If you dont use guice, have a look at needle4j. Its a test-support lib that automatically injects mocks when injection is required. But it works only with easymock or mockito.

Applying multiple permutations of mocks to dependencies for better unit testing in Spring

Let's say I have a class (OrchestratingClass) that has a method orchestrate() that calls a number of smaller methods of other classes (classA's do1() method, classB's do2() method). I would like to test the behavior of orchestrate() by mocking the responses of do1() and do2() with various permutations. I'm running my test with something like:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration
public OrchestratingClassTest {
#Inject
private OrchestratingClass oc;
#Test
public void test1() {
// I would like to mock classA's do1() method to send back "1"
// I would like to mock classB's do2() method to send back "2"
}
#Test
public void test2() {
// I would like to mock classA's do1() method to send back "100"
// I would like to mock classB's do2() method to send back "200"
}
static class SimpleConfig {
#Bean
public InterfaceA interfaceA() {
return new ClassA();
}
#Bean
public InterfaceB interfaceB() {
return new ClassB();
}
#Bean
public OrchestratingClass orchestratingClass() {
return new OrchestratingClass();
}
}
}
And the orchestratingClass itself is quite basic, but I've added some sample code to aid in visualization:
#Named
public OrchestratingClass {
#Inject
private InterfaceA interfaceA;
#Inject
private InterfaceB interfaceB;
public String orchestrate() {
return interfaceA.do1() + " " + interfaceB.do2();
}
}
Now I'm aware I could tweak my SimpleConfig class to have the mocked out versions of classA and classB, but then I'm locked into 1 particular mock and can't "re-mock" things when I move onto test2(). I'm convinced that playing around with the java config files for 1 single test class would not work if we're trying to inject different "flavors" of beans "per-test". Does anyone have any recommendation on what I can do to make sure I'm really testing this thoroughly without being invasive (ex: adding superfluous "setters" for the specific classes in the orchestratingClass to side-step the bean injection pain)? Essentially, I'm looking to "tamper" the applicationContext on a per-test basis for specific beans of interest (along with the necessary housekeeping that's required) by applying a variety of mocks.
Here's a simple example using Mockito:
public class OrchestratingClassTest {
#Mock
private ClassA mockA;
#Mock
private ClassB mockB;
#InjectMocks
private OrchestratingClass oc;
#Before
public void prepare() {
MockitoAnnotations.initMocks(this);
}
#Test
public void shouldConcatenate() {
when(mockA.do1()).thenReturn("1");
when(mockB.do2()).thenReturn("2");
assertEquals("1 2", oc.orchestrate());
}
}
The magic happens in the call to MockitoAnnotations.initMocks(this), which will create mock instances for the fields annotated with #Mock, then create a real instance of Orchestrating class and inject its fields, thanks to #InjectMocks.
Note that, even without this magic, you could make your class easily testable by just adding a constructor taking ClassA and ClassB as arguments to OrchestratingClass, and annotate that constructor with #Autowired instead of annotating the fields. So, in short, use constructor injection rather than field injection.

Categories