Showing posts with label junit. Show all posts
Showing posts with label junit. Show all posts

09 October, 2016

java.lang.Error: Unresolved compilation problems

java.lang.Error: Unresolved compilation problems:



Facing issue is often hurdle for developer :) :) As you all know, Java Developers Guide always posted the realtime issues. This issue I have faced while writing Junit test cases, but its not so easy to fix your development desktop. The below issue normally happens when the class path is missed the related weblogic jar.

I am using weblogic for jaxrpc client calls, and its failing to find the weblogic jar. Below error logs :
  

java.lang.Error: Unresolved compilation problems:
    The type weblogic.wsee.jaxrpc.ServiceImpl cannot be resolved. It is indirectly referenced from required .class files
    The type weblogic.wsee.jaxrpc.StubImpl cannot be resolved. It is indirectly referenced from required .class files
    The import weblogic.wsee.security cannot be resolved
    The import weblogic.xml cannot be resolved
    The import weblogic.xml cannot be resolved
    The method addCredentialsToSecurityHeader(Stub, HandlerInfo) in the type JdcNotificationHandler is not applicable for the arguments (JdcEnablerService_PortType_Stub, HandlerInfo)
    The method addCredentialsToSecurityHeader(Stub, HandlerInfo) in the type JdcNotificationHandler is not applicable for the arguments (JdcEnablerService_PortType_Stub, HandlerInfo)
    The method addCredentialsToSecurityHeader(Stub, HandlerInfo) in the type JdcNotificationHandler is not applicable for the arguments (JdcEnablerService_PortType_Stub, HandlerInfo)
    CredentialProvider cannot be resolved to a type
    CredentialProvider cannot be resolved to a type
    CredentialProvider cannot be resolved to a type
    ClientUNTCredentialProvider cannot be resolved to a type
    WSSecurityContext cannot be resolved to a variable

    at au.com.mycompany.mcas.sdp.bizservice.user.domain.util.notification.JdcNotificationHandler.<init>(JdcNotificationHandler.java:1)
    at au.com.mycompany.mcas.sdp.bizservice.user.application.facade.spring.handler.JdcNotificationHandlerTest.<init>(JdcNotificationHandlerTest.java:31)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.runners.JUnit4ClassRunner.createTest(JUnit4ClassRunner.java:72)
    at org.mockito.internal.runners.JUnit44RunnerImpl$1.createTest(JUnit44RunnerImpl.java:26)
    at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:79)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at org.mockito.internal.runners.JUnit44RunnerImpl.run(JUnit44RunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)



How to fix  :- This is very simple, just add the weblogic.jar library jar into your class path. Steps as below,


Right Click on Project => Build Path => Libraries and then add the required jar (In my case I have added weblogic.jar).

The screenshot attached for reference. 


java.lang.Error



Hope it will help you.




Follow for more details on Google+ and @Facebook!!!

Find More Issues & Solutions -







15 August, 2016

How to mock private fields of a class for JUnit testing

How to mock private fields of a class for JUnit testing ?



This is really very tricky and major concern for developers. Specially who are not following TDD(Test Driven Developement ) process.

Of course it not the challenge for java developer to get this done, but when there is a time period to deliver your project. ohh god !!! you only can save me.


By the way , I also faced same issue with my recent project. After some kind of investigation .. and its weird. 


Actully I was using Junit with Mockito. I tried with mockito , but no luck. Hence mockito will not allow you to set the private fields . 

Finally I tried with Reflection to enforce the private variable to set the data using ReflectionTestUtils.
import org.springframework.test.util.ReflectionTestUtils;
 

Sample code fix using Junit Test (Reflection API) :-

/**
 * This class contains your private variables those you want to mock.
 */

public class MyObjectImplImpl {
    private String fieldName1;
    private String fieldName2;

}

Test Class (MyObjectImplTest.java):-

public class MyObjectImplTest {

    @InjectMocks
    MyObjectImplImpl myObjectImplImpl;

    @Before
    public void before() {
        myObjectImplImpl = mock(MyObjectImplImpl.class);
        myObjectImplImpl = new MyObjectImplImpl();

        //Set the private variable values here by reflection.
        ReflectionTestUtils.setField(myObjectImplImpl, "fieldName1", "setnew-field-value-1");
        ReflectionTestUtils.setField(myObjectImplImpl, "fieldName2", "setnew-field-value-2");
    }

    

    /**
     * Your Test Method.
     */
    @Test
    public void testJavaDeveloperGuide() {
       //do your test case here.
    }

}


This is working fine for me.


Either the alternative is you can try powermock , instead of mockito. Powermock is working for this issue. This will fix this private field access issue. Also its more powerful than mockito.



Happy coding.........!!!


Hope this will help you and will save your all day :::)



16 July, 2015

mocking is Null after @InjectMocks

Mocking is null after injecting the mock, this issue is very common in mockito. I faced this issue when trying to write the junit test case . 

This issue seems you need to load the test class using MockitoAnnotations.initMocks. Because , there is certain steps you need to do / setup the data before executing the test method.

@InjectMocks
    private TokenServiceFormatter tokenServiceFormatter ;


As above I have already injected the respective class, that I want to inject. But, while running its showing the tokenServiceFormatter  (injected object ) is null. So, inside the setup() method you need to load the test class as below.


    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);    
    }

 @Test
public void testMethod(){
//Do your test here
}




Happy Mockinggggggggggggg.