I want the inherited getters in class A to return Mock of MyObject. How can I do that without directly stubbing getMyObject() method in A (i.e. via using some MockBase class) ?
public class Base {
#Autowired
private MyObject obj;
public MyObject getMyObject(){
return obj;
}
}
public class A extends Base {
#Autowired
private SomeObject xyz;
public void myMethod(){
MyObject obj = getMyObject();
//do something here
}
}
public void MockBase {
#Mock
protected MyObject obj;
public MyObject getMyObject(){
return obj;
}
}
public class ATest extends MockBase {
#InjectMocks
private A a;
#Mock
private SomeObject xyz;
#BeforeMethod
public void setUp(){
MockitoAnnotations.initMocks(this);
}
public void MyMethod_SomeCondition_ExpectedResult{
a.myMethod();
}
}
What's the issue there ? It's working as expected, maybe you ar using an older version of Mockito. For example with version 1.9.5 :
Heres's the parent of the class you want to test :
public class Base {
private BaseObjectDependency base_object_dependency;
public BaseObjectDependency getBase_object_dependency() {
return base_object_dependency;
}
}
Here's the class you want to test :
public class Child extends Base {
private ChildObjectDependency child_object_dependency;
public void myMethod() {
BaseObjectDependency obj = getBase_object_dependency();
//do something here
}
public ChildObjectDependency getChild_object_dependency() {
return child_object_dependency;
}
}
Here are the dependencies :
public class BaseObjectDependency { }
public class ChildObjectDependency { }
Now here's the parent class of the test :
import org.mockito.Mock;
public class MockBase {
#Mock protected BaseObjectDependency base_test_dependency_mock;
}
And finally the test :
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
public class ChildTest extends MockBase {
#InjectMocks private Child child;
#Mock private ChildObjectDependency child_test_dependency_mock;
#BeforeClass
public void setUp() {
MockitoAnnotations.initMocks(this);
}
#Test
public void check_that_mocks_are_correctly_injected() {
assertNotNull(child.getBase_object_dependency());
assertEquals(base_test_dependency_mock, child.getBase_object_dependency());
assertNotNull(child.getChild_object_dependency());
assertEquals(child_test_dependency_mock, child.getChild_object_dependency());
}
}
You'll see that nowhere I am stubbing getBase_object_dependency().
Related
This is what I want:
import com.abc.ClassA;
public class TestClass {
private ClassA firstClass;
private com.xyz.ClassA secondClass;
}
This is what I got:
public class TestClass {
private com.abc.ClassA firstClass;
private com.xyz.ClassA secondClass;
}
I am using com.helger.jcodemodel with CodeModel.ref("com.abc.ClassA") and CodeModel.ref("com.xyz.ClassA")
I have this Interface:
public interface Test<T> {
default Class<?> getT() {
return T.getClass(); < --error
}
}
next i have a class that implements it:
static class ItemService implements Test<Item>{
}
And i want to get the 'Item' class from the 'ItemService' class
static ItemService service = new ItemService();
private static void k() {
System.out.println(service.getT());
}
Now one way to do it is this:
public interface Test<T> {
default Class<?> getT() {
return Type.type;
}
class Type {
public static Class<?> type;
}
}
Service:
static class ItemService implements Test<Item> {
public ItemService() {
Type.type = Item.class;
}
}
And it works fine but there is a problem,
When another class implement the interface:
static class OrderService implements Test<Order> {
public OrderService() {
Type.type = Order.class;
}
}
And i try:
static ItemService service = new ItemService();
static OrderService orderservice = new OrderService();
private static void k() {
System.out.println(service.getT());
}
I get the Order class and not the Item class
How can i make it work?
Classes inside interfaces are static, You can remove the default from the function and every class will need to implement this. example:
public interface Test<T> {
public Class<T> getT();
}
static class ItemService implements Test<Item> {
public Class<Item> getT() {return Item.class;}
}
static class OrderService implements Test<Order>{
public Class<Order> getT() {return Order.class;}
}
An alternative could be an abstract class.
public interface Test<T> {
public Class<T> getT();
}
abstract class AbstractTest<T> implements Test<T> {
private final Class<T> type;
AbstractItemService(Class<T> type) { this.type = type }
public Class<T> getT() {return type;}
}
class ItemService extends AbstractTest<Item> {
ItemService() { super(Item.class); }
// implement other things
}
class OrderService extends AbstractTest<Order>{
OrderService() { super(Order.class); }
// implement other things
}
Here is another option, if your implementation has an instance of T.
interface Test<T>{
T getT();
default Class<?> getClassOfT(){
return getT().getClass();
}
}
Using Powermock 2.0.7 (powermock-api-mockito2, powermock-core, powermock-module-junit4) and mockito-core (3.3.3). I thought I had created a comparable test scenario in a separate project (which worked), but something else must be missing.
Library class to be mocked:
public class CommonConstants {
private ConfigurationDataImpl configurationData;
private static Properties sysProperties;
private static Map<String, String> sysPermissions;
public CommonConstants(ConfigurationDataImpl configurationData) {
this.configurationData = configurationData;
}
public void init() {
sysProperties = this.configurationData.getSysParams();
sysPermissions = this.configurationData.getSysPermissions();
}
public static String getSysProperties(String key) {
return sysProperties.getProperty(key);
}
public static String getSysPermissions(String key) {
return (String)sysPermissions.get(key);
}
}
In my test, I have:
#RunWith(PowerMockRunner.class)
#PrepareForTest({CommonConstants.class}) <==== class that evidently is not modified!
class MyServiceTest {
public MyServiceTest() {}
#Mock
private MyDao myDao;
#InjectMocks
private MyService myService;
#Test
void retrieveUsers() {
RequestPayload rp = returnPayload();
PowerMockito.mockStatic(CommonConstants.class); <==== EXCEPTION
when(CommonConstants.getSysProperties(HOURS_TO_REGISTER)).thenReturn("24");
...
In the service code, I have:
#Service
#Slf4j
public class MyService {
MyDao myDao;
public MyService(MyDao myDao) {
this.myDao = myDao;
}
public UserListResponse retrieveUsers(RequestPayload requestPayload, String customer) {
List<User> users = myDao.getPtdUsers(queryParams, customer, totalRecords);
int hoursToExpire = Integer.parseInt(CommonConstants.getSysProperties(HOURS__TO_REGISTER));
...
Am I leaving out something? All help appreciated.
How can we unit test private class ?
For example with an example of private class #Autowired with qualifier
I would like to verify if the good qualifier is call
public class MyClass {
#Autowired
IHelloService helloService;
public void sayHello(List<Person> list) {
for(Person person : list) {
helloService.sayHello(person);
}
}
}
.
#Primary
#Component
public class SayHelloService implements ISayHello {
#Autowired
#Qualifier("french")
ISayHello french;
#Autowired
#Qualifier("english")
ISayHello english;
#Override
public void sayHello(Person person) {
switch (person.getLanguage) {
case "EN":
english.sayHello(Person person);
break;
case "FR":
french.sayHello(Person person);
break;
default:
break;
}
}
}
.
#Qualifier("french")
Component
class SayHelloFrenchService implements ISayHello {
public void sayHello(Person person) {
sysout("Bonjour " + person.getName());
}
}
#Qualifier("english")
Component
class SayHelloFrenchService implements ISayHello {
public void sayHello(Person person) {
sysout("Hello " + person.getName());
}
}
Edit:
I failed my example: the twice qualifier class were private
If i #Mock the interface it works.
I thought i must #Mock the implementation...
But i can't write tests of implementation of private class.
How to mock super method call from mocked method
public class A extends B{
#Override
public retrieveData(){
//some action here
super.retrieveData();
}
}
abstract class B extends C{
//init super fields here
}
public class C{
public String retrieveData(){
//some return
}
}
Public Atest{
#InjectMock
A a;
#Mock
C parent; //also tryed for B but the same effect
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
#Test
public void testDetailsRetrievePageList(){
when(parent.retrieveData()).thenReturn("stub");
String s = a.retrieveListData();
Assert.assertEquals("stub", s);
}
}
Didn't help Mockito How to mock only the call of a method of the superclass and post like that.
everytime my super is null in A class