My First Post      My Facebook Profile      My MeOnShow Profile      W3LC Facebook Page      Learners Consortium Group      Job Portal      Shopping @Yeyhi.com

Pages










Thursday, December 3, 2020

Mockito Vs EasyMock - How to use and the difference in Unit Testing

What is mocking? 

A unit test should be independent of any external resources : database, message queue etc.

Ideally, a unit test should test only one class. Mocking is a best practice when testing an object which is linked to other objects.

Example : CompanyCreateTest should only test CompanyCreate


Mockito and EasyMock:


Both are java mocking libraries for Unit Testing, and follow the same model of

  1. Mock external dependencies
  2. Setup expectations
  3. Run test
  4. Verify results

Both of the above are equivalent in features and capabilities. The essential difference exists in their usage at point 3 and 4. EasyMock needs explicit replaying and verification of Mocks and Mockito doesn't. 

Similarities :

  • Mocks concrete classes as well as interfaces
  • Supports exact-number-of-times and at-least-once verification
  • Argument matchers
  • Annotation support

Remember that for Mocking, we will use mocks in our object. So, if you are using interfaces and IOC you can write your own fake implementations and inject it in your tested object. However, writing fake implementations for every linked objects could be very fastidious. That's why we will use mock libraries.


Differences between both:

Mockito has a simplified api that results in much flatter learning curve and ease of writing and maintainability. A great benefit outlined above is the developer doesn't need to call out what mocks need to be replayed. This is handled implicitly in Mockito. That compared to Easy Mock, the learning curve is higher and results in confusion several times when new mocks are added/deleted, all the explicit references need to be accordingly handled. EasyMock results in more code than Mockito to test the same functionality.

 

A simplified EasyMock example below :

TestClassA {
 
void setup(){
//Create mock
ClassBeingMocked mock1 = EasyMock.createMock(ClassBeingMocked);
}
 
 
void testMethod(){
//Setup expectations
EasyMock.expect(mock1.getAge()).andReturn(10);
//Replay mode
EasyMock.replay(mock1);
//Test
ClassBeingTested source = new ClassBeingTested();
source.calculateAge(mock1);
//Assertions
assertEquals(50, source.getAverageAge());
//Verification mode
EasyMock.verify(mock1);
}
}

Same example in Mockito

TestClassA {
 
 
void setup(){
//Create mock
ClassBeingMocked mock1 = Mockito.Mock(ClassBeingMocked);
}
 
 
void testMethod(){
//Setup expectations
Mockito.when(mock1.getAge()).thenReturn(10);
 
//Test
ClassBeingTested source = new ClassBeingTested();
source.calculateAge(mock1);
 
//Assertions
assertEquals(50, source.getAverageAge());
 
//Verification mode
Mockito.verify(source).someInternalMethodCalled();
}
}

Using Mockito:

Definition

Mockito is a Java library used to mock objects.

Basically, you can write the following to create a fake implementation of a List :

List mockedList = mock(List.class);

And you can specify the expected behaviour of your mock for your test case :

when(mockedList.get(0)).thenReturn("first");

MockitoJUnitRunner

Mockito comes with a JunitRunner you could enable like that :

@RunWith(MockitoJUnitRunner.class)
public class TestDefaultRule
{
    @InjectMocks
    private DefaultRule defaultRule = new DefaultRule();
     
    @Mock
    private AirProductManager apManager;
     
    @Mock
    private AirProduct airProduct ;
...
}

Doing that, Mockito will create all your mocks for every objects annotated with @Mock and inject those mocks in DefaultRule that was annotated with @InjectMocks.

Inject mock without setters

The annotation @InjectMocks is very usefull when your object do not expose setter as for the object DefaultRule :

public class DefaultRule implements MarkupRule {
...
    @Autowired
    private AirProductManager airProductManager;
...

airProductManager is private and has no setters, so without the annotation @InjectMocks you would have used very tricky method to inject your mock manually.

Initialize value without setters

Sometimes, you could have some objects initialized by @Value annotation and without setters.
Exemple in TneManager :

public class TneManagerImpl implements TneManager
{
    @Value("${DimoAuthentificationCryptingStrategy.PassKey}")
    private String dimoPassKey;
    @Value("${EolAuthentificationCryptingStrategy.PassKey}")
    private String eolPassKey;
...
}

In this situation, you have a solution using ReflectionTestUtils from Spring in your setup method :

@Before
public void initialize ()
{
    ReflectionTestUtils.setField(tne, "dimoPassKey", "dimo");
    ReflectionTestUtils.setField(tne, "eolPassKey", "eol");
}

Mock static method

Mockito is not able to mock static method by itself. However when using static method you can mix Mockito and PowerMock.

To enable PowerMock you have to use the runner PowerMockRunner.

Example in AirThirdPartyLinkImplMockitoTest :

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Lookup.class })
@PowerMockIgnore("org.apache.commons.logging.*")
public class AirThirdPartyLinkImplMockitoTest
{
...
    @Before
    public void initialize() throws Exception
    {
        // As you don't use MockitoRunner, you have to initialize all mocks with the following line
        MockitoAnnotations.initMocks(this);
...
    // o use PowerMockito to mock the static call of Lookup.getInstance()
    // A lot of stuff just to use Carbon...
    // -------------------------------------------------------------------
    PowerMockito.mockStatic(Lookup.class);
        Lookup lookup = mock(Lookup.class);
        when(Lookup.getInstance()).thenReturn(lookup);
        PNRConstructManager mgr = mock(PNRConstructManager.class, withSettings().extraInterfaces(Component.class));
        when(lookup.fetchComponent(PNRConstructManager.DEFAULT_COMPONENT_PATH)).thenReturn((Component)mgr);
 
    }

Explanations :

Enable the runner :

@RunWith(PowerMockRunner.class)

Explicitely tell which object will be mocked

@PrepareForTest({ Lookup.class })

The following line is needed in the setup method. Otherwise every annotations Mock and InjectMock will be ignored as you don't use MockitoJUnitRunner

MockitoAnnotations.initMocks(this);

and the mock of the static method :

PowerMockito.mockStatic(Lookup.class);
    Lookup lookup = mock(Lookup.class);
    when(Lookup.getInstance()).thenReturn(lookup );

Monday, November 30, 2020

Mockito MockMaker Java IllegalStateException Error: How to Fix Mockito Plugin error in Testing

 There are times when you will land into problems while running Mock tests using Mockito. A Sample error could be like:

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker


The issue is that the Mockito core depends on a library called byte-buddy and this problem is mostly occurred when mockito doesn’t find a matching byte buddy jar version.

Error screenshot:


To Solve this issue find out the mockito core version your project is using.

In eclipse, you can check in project build path by navigating as follows:

Right click on project -> Properties -> Java build path -> Libraries tab


For IntelliJ This could be different. Look the screenshot below:



Search maven repository for that version of mockito core. To me, it was : https://mvnrepository.com/artifact/org.mockito/mockito-core/2.7.1



Description of issue:

The actual issue it seems is because of error from ByteBuddyAgent. I found following stacktraces that pointed me to the error:

Caused by: java.lang.IllegalStateException: Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@e32787d

at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:316)

at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<clinit>(InlineByteBuddyMockMaker.java:102)

... 34 more

Caused by: java.lang.reflect.InvocationTargetException

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at net.bytebuddy.agent.ByteBuddyAgent.install(ByteBuddyAgent.java:303)


Solution to fix the error:

Look at the Compile Dependencies section. Note down the correct dependent version of byte-buddy and include in the project. If the jar is already included with some other version, override the version with this correct version.

Also, i did see following in stacktrace:

[ERROR] error: error while loading Object, Missing dependency 'object scala in compiler mirror', required by /modules/java.base/java/lang/Object.class


So, fix above two issues. Build the application and run the tests again. Your issue related to MockMaker must be solved by now.

Voila :)

Enjoy!


Monday, November 23, 2020

Update Variables using BAsh_Profile in MAC/LInux

Sometimes you need to update the environment variables permanently. I have found setting JAVA_HOME and M2_HOME as an important and recurring requirement while working in Adobe, Symantec and even at Expedia Group. I am listing the steps here. Its simple :)


Steps:

  1. Open Bash profile: vi ~/.bash_profile
  2. Append: export M2_HOME=/Users/mfaiz/Projects/apache-maven-3.6.0/bin
  3. export PATH=$PATH:$M2_HOME
  4. export JAVA_HOME=/Users/mfaiz/my-jdk/Mac
  5. On terminal run: source ~/.bash_profile
  6. You can verify the install using: mvn - - version (This will show you version eg 3.6.0 in my case)


Thursday, November 5, 2020

Market Segmentation Strategies in MAJF Technologies in Hindi Speaking Belt: [A Case Study by W3LC]

व्यवसाय में ऐसा करने की प्रवृत्ति है - बहुत से उद्यमी हर अवसर का पीछा करना चाहते हैं और यथासंभव व्यापक बाजार की अपील करते हैं। व्यवसायी और उनके नेता, किसी ऐसे उत्पाद या सेवा को शुरू करने के लिए दबाव महसूस करते हैं, जो उनके रणनीतिक लक्ष्यों और मुख्य दक्षताओं के बाहर बैठे दर्शकों से अपील करता है। आपके ग्राहक कौन हैं और आप उन्हें क्या प्रदान करते हैं, इसकी स्पष्ट समझ का मिलान करना महत्वपूर्ण है। MAJF Technologies मामले में महत्वपूर्ण उपयोगकर्ता अनुभव पर पूरी तरह ध्यान केंद्रित - यह एक महान निर्णय था। इन तीन कारणों पर विचार करें कि क्यों आपके व्यवसाय को कई उत्पादों, सेवाओं, समाधानों या ग्राहकों को सामान्य बनाने के बजाय एक विशेषता पर ध्यान केंद्रित करना चाहिए।


विभिन्न समूहों की जरूरतों और चाहतों को बेहतर तरीके से संबोधित करने के अलावा, इस रणनीति से अन्य लाभ भी हैं। एक कंपनी जो अपने लक्षित ग्राहक क्षेत्रों का लाभ उठाती है, वह विपणन विफलता के जोखिम को काफी कम कर सकती है। इसके बजाय, वे कहां, कब, कौन और कैसे मार्केटिंग के बारे में अधिक सटीक निर्णय ले सकते हैं। चूंकि कंपनियों के पास विपणन पर खर्च करने के लिए सीमित संसाधन होते हैं, इसलिए प्रत्येक समूह के लिए किए गए विकल्प बेहतर प्रतिक्रिया और राजस्व में वृद्धि कर सकते हैं।


इससे कोई फर्क नहीं पड़ता कि आप अपने लक्षित बाजार सेगमेंट से सबसे बड़ा लाभ पाने के लिए क्या दृष्टिकोण अपनाते हैं, आपको यह तय करना होगा कि किस प्रकार का विभाजन करना है। व्यापक रूप से इस्तेमाल किए गए विभाजन तरीकों में शामिल हैं:

भौगोलिक विभाजन में देश, क्षेत्र या राज्य द्वारा बड़े समूह शामिल हैं। इसमें शहर, पड़ोस और यहां तक ​​कि ज़िप कोड द्वारा विभाजित अधिक केंद्रित समूह भी शामिल हैं। यदि आप उत्पाद रेंज बेच रहे हैं तो इस तरह का विभाजन फायदेमंद है जो विभिन्न जलवायु आवश्यकताओं को संबोधित करता है। वैकल्पिक रूप से, भौगोलिक क्षेत्रों में विशिष्ट आर्थिक या सांस्कृतिक आवश्यकताएं हो सकती हैं जो विपणन के लिए महत्वपूर्ण कारक हैं।

जनसांख्यिकी विभाजन में आयु, व्यवसाय, शिक्षा, धर्म, नस्ल या जातीयता, लिंग, आय और पारिवारिक आकार जैसे कारक शामिल हैं। कई कारक विशिष्ट आवश्यकताओं, लिंक और क्रय शक्ति से जुड़ते हैं।

व्यवहार विभाजन एक विशेष उत्पाद या सेवा के जवाब में विभिन्न समूहों के ज्ञान और दृष्टिकोण पर विचार करता है। यह जानकारी पिछले खरीद का अध्ययन करने से एकत्र की जाती है।

MAJF Technologies यह जानती है। और, यह प्रभावी तरीके से अपनी मूल शक्ति और ग्राहकों पर ध्यान केंद्रित करने की कोशिश करती है।


1. ग्राहक प्रामाणिकता बनाम पागलपन को जानते हैं

जब आप एक ग्राहक आधार पर ध्यान केंद्रित करते हैं, और आप उस ग्राहक के साथ निकटता से जुड़ जाते हैं, तो आप वास्तव में उनके साथ प्रतिध्वनित हो सकते हैं और विश्वास का निर्माण कर सकते हैं। ग्राहक आपकी प्रामाणिकता को महसूस कर सकते हैं और एक बंधन में ध्यान केंद्रित कर सकते हैं।

विपरीत रूप से, कई प्रकार के ग्राहकों से अपील करने का प्रयास डिस्कनेक्ट कर सकता है। यदि आप वास्तव में ग्राहक के साथ गठबंधन नहीं करते हैं, या वास्तव में उनके अनुभवों या रुचियों को जानते हैं, तो वे आपकी जिद का पता लगाएंगे। साथ ही, कई चीजों के विशेषज्ञ होने का दावा करना आपके संदेश को कमजोर कर सकता है। कई चीजों में विशेषज्ञ होना संभव नहीं है, और इससे संदेह पैदा होता है।

ग्राहकों के साथ जुड़ने में प्रामाणिकता कैसे मदद कर सकती है इसका एक उदाहरण पर्पल कैरोट, एक भोजन-किट वितरण सेवा है। जबकि अन्य सेवाओं में खाना पकाने या किराने की सदस्यता पर ध्यान केंद्रित किया गया था, पर्पल कैरोट विशेष रूप से शाकाहारी भोजन विकल्पों पर ध्यान केंद्रित करके एक जगह बनाने में सक्षम था। अगर कंपनी मांस या डेयरी वाले खाद्य पदार्थों की एक विस्तृत चयन की पेशकश करते हुए एक शाकाहारी जीवन शैली के लाभों को टाल देती थी, तो ग्राहक इसे अमानवीय मानते थे। अपने उद्देश्य और आला से चिपके हुए, वे ग्राहकों के साथ विश्वास का निर्माण कर सकते हैं और शाकाहारी समुदाय का एक बड़ा बाजार हिस्सा हड़प सकते हैं।


2. ग्राहक एक विशेष प्रदाता की विशेषज्ञता पर भरोसा करते हैं

एक व्यवसाय विकास अधिकारी के रूप में बी 2 बी (बिजनेस-टू-बिजनेस) बिक्री पर ध्यान केंद्रित करने वाले मेरे अनुभवों में, मैंने सीखा कि संभावित ग्राहक एक निश्चित काम करने के लिए आपके उत्पाद या सेवा को काम पर रख रहे हैं। किसी सौदे को बंद करने से पहले, उन्हें आपके और आपके समाधान में एक उच्च स्तर के विश्वास तक पहुंचना चाहिए। विशेषता प्रदाता अपने सामान्यवादी समकक्षों की तुलना में आत्मविश्वास को प्रेरित करने की अधिक संभावना रखते हैं क्योंकि वे एक उत्पाद या सेवा पर ध्यान केंद्रित करते हैं, और यह ध्यान ग्राहक की जरूरतों को पूरा करता है।

ऐसा ही एक उदाहरण CARR है, जो एक रियल एस्टेट व्यवसाय है जो विशेष रूप से हेल्थकेयर किरायेदारों और खरीदारों पर केंद्रित है। एक औसत ब्रोकर होने के बजाय जो कभी-कभी चिकित्सीय-विशिष्ट गुण बेचता है, सीएआरआर एक बेहतर मूल्य प्रस्ताव पेश कर सकता है क्योंकि वे जानते हैं कि आला औसत ब्रोकर से बेहतर है।

मेरे व्यवसाय के मामले में, हमने पूरी तरह से वीडियो डोरबेल पर ध्यान केंद्रित किया, और स्मार्ट ताले या स्मार्ट लाइट के साथ अन्य अवसरों का पीछा नहीं किया। हमारे ग्राहक भरोसा कर सकते थे कि हम अपना सारा ध्यान और संसाधन डोरबेल पर लगा रहे थे - और हम विशेषज्ञ थे।

यदि आप एक गैर-व्यावसायिक उदाहरण चाहते हैं, तो माइकल जॉर्डन पर विचार करें। यकीनन सभी समय का सबसे बड़ा बास्केटबॉल खिलाड़ी होने के बावजूद, जॉर्डन पेशेवर बेसबॉल खेलने का प्रयास करते समय मामूली लीग से परे जाने में असमर्थ था। हर चीज में महान होना बेहद मुश्किल है, और हम जिन लोगों को महान मानते हैं उनमें से कई एक बात पर ध्यान केंद्रित करते हैं।


3. व्यापक विपणन प्रयास कम प्रभावी हैं

पहले के वर्षों में जब मैं एक उद्यमी था (दस साल पहले), मैंने ग्राहकों को एसईओ (सर्च इंजन ऑप्टिमाइजेशन) के साथ ऑनलाइन उपस्थिति स्थापित करने और ऑनलाइन मार्केटिंग के माध्यम से राजस्व बढ़ाने में मदद की। अब तक मैंने जो सबसे बड़ी गलती की थी, वह कई ग्राहक खंडों, विज्ञापन अभियानों या मार्केटिंग चैनलों पर मार्केटिंग बजट फैला रही थी।

व्यापक ऑडियंस सेटिंग, या बहुत सारे अभियानों के साथ ऑनलाइन मार्केटिंग अभियान, अत्यधिक केंद्रित होने वाले अभियानों के साथ-साथ प्रदर्शन नहीं करते थे। जब आपका विपणन किसी विशिष्ट प्लेटफ़ॉर्म पर किसी विशिष्ट ग्राहक के लिए खाता करने के लिए पर्याप्त रूप से केंद्रित होता है, तो आपका विज्ञापन ग्राहक के साथ और अधिक मजबूती से प्रतिध्वनित होगा और आप बेहतर ROI बनाम केवल अधिक से अधिक स्थानों पर जेनेरिक विज्ञापन रखने देखेंगे।

इसी तरह, पारंपरिक विज्ञापन रणनीति जैसे कि टेलीविजन विज्ञापन, रेडियो स्पॉट और प्रिंट अभियान भी लक्षित ऑनलाइन विपणन अभियानों के रूप में प्रभावी नहीं हैं। इसका एक बड़ा उदाहरण YouTube विज्ञापन में सुपर बाउल वाणिज्यिक बनाम है। ब्रांड 30 मिलियन से अधिक स्थानों पर 100 मिलियन से अधिक दर्शकों को खेल देखने के लिए विज्ञापित करने के लिए $ 5 मिलियन से अधिक खर्च करते हैं। YouTube पर, हर दिन 1 बिलियन से अधिक मोबाइल-व्यू होते हैं। इसका मतलब है कि YouTube पर हर दिन दस सुपर बाउल समकक्ष हो रहे हैं, केवल मोबाइल पर।

पिछले साल, जीप एक सुपर बाउल विज्ञापन के बदले में 106 मिलियन विचारों को ऑनलाइन करने में सक्षम थी। यह दिखाता है कि आप पारंपरिक विज्ञापन समाधानों के माध्यम से प्रसारण की तुलना में ऑनलाइन बड़े दर्शकों तक कैसे पहुँच सकते हैं।


अंतिम शब्द

एक मुख्य योग्यता और मूल्य प्रस्ताव में विशेषज्ञता से आप अपने समाधान को बेहतर स्थिति में ला सकते हैं और अपने क्षेत्र में ब्रांड पहचान स्थापित कर सकते हैं। हर अवसर का पीछा करने के बजाय, अपने मुख्य ग्राहक कौन हैं और उनकी जरूरतों और इच्छाओं को प्राथमिकता देने के लिए अपने प्रयासों पर ध्यान केंद्रित करें। उस ध्यान को अपने व्यावसायिक निर्णयों को चलाने की अनुमति दें, और आप भीड़ से अलग खड़े होंगे। एक बार जब किसी ब्रांड का अपने उत्पादों या सेवाओं के लिए एक सामान्य लक्ष्य बाजार होता है, तो वे इसे सेगमेंट कर सकते हैं। बाजार विभाजन का समग्र लाभ यह है कि अब खरीदारों के समूह हैं जिन्हें उनकी समान जरूरतों और चाहतों द्वारा परिभाषित किया गया है। ऐसा करने में, एक कंपनी खरीदार-आधारित विपणन रणनीति के माध्यम से निवेश पर अपनी वापसी में सुधार कर सकती है।


Submitted By: Marketing Interns, 2019 Batch

Research Area: Eastern UP, India ( AProminently Hindi Speaking Belt)

Monday, October 12, 2020

Using different Versions of Node using NVM

 To check your current active version of Node and NPM

node -v
npm -v

Node 10.7.0

Node is most easily managed using NVM and that is the recommended tool we suggest. https://github.com/creationix/nvm



Now, suppose you have different version of node installed, but some app needs a lower or a different version. Then what to do. Here comes nvm for the rescue!


nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.

Install & Update Script

To install or update nvm, you should run the install script. To do that, you may either download and run the script manually, or use the following cURL or Wget command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash


Do not forget to source the bash profile after this:

Simply type source ~/.bash_profile

Alternatively, if you like saving keystrokes you can type . ~/.bash_profile


Now you can use nvm commands to change node version

eg:

nvm install 10.7

or, nvm use 10.7