java - Mock final method in inner class with PowerMockito -


i'm having trouble stubbing final method, used inside of class i'm trying test. problematic line in test class here:

powermockito.when(connectionmock.authuser("fake-user", "fake-password")).thenreturn("random string"); 

which throws nullpointerexception. test class looks this:

@runwith(powermockrunner.class) @preparefortest({myclass.class, apiclientconnection.class}) public class myclasstest {     @test     public void apiconnect() throws exception {         myclass c = new myclass();         apiclientconnection connectionmock = powermockito.mock(apiclientconnection.class);         powermockito.whennew(apiclientconnection.class).withanyarguments().thenreturn(connectionmock);         powermockito.when(connectionmock.authuser("fake-user", "fake-password")).thenreturn("random string");         c.apiconnect("fake-host", 80, "fake-user", "fake-password");     } } 

the class i'm testing follows:

public class myclass {     public myclass() { }     public apiclientconnection apiconnect(string host, int port, string user, string pass) {             conn = new apiclientconnection(host, port);             conn.authuser(user, pass);     } } 

where authuser() defined final so:

public class apiclientconnection {     public final string authuser(string username, string password) {         ...     } } 

i'm following along how mock non static methods using powermock , can powermockito mock final method in non-final concrete class?. i've tried variations such using mockito instead of powermock stub authuser, , adding apiclientconnection.class preparefortest annotation. can't figure out why isn't working though. doing wrong?

your problem here

powermockito.when(connectionmock.authuser("user", "password")).thenreturn("random string"); c.apiconnect("fake-host", 80, "fake-user", "fake-password"); 

you indicating method should stubbed when user logs credentials user/password, sending fake-user/fake-password.

replace last line c.apiconnect("fake-host", 80, "user", "password");


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -