logo
down
shadow

Mocked class function being called instead of only being mocked


Mocked class function being called instead of only being mocked

By : user2948540
Date : November 14 2020, 04:51 PM
should help you out I am mocking a class in spock and just want to check whether the methods inside the method being tested are getting called or not , I don't want internal methods to run. , The following code works perfectly fine, it fails with:
code :
JUnit 4 Runner, Tests: 1, Failures: 1, Time: 86
Test Failure: process code test(Test)
Too few invocations for:

1 * codeProcessor.encodeMessage(request)   (0 invocations)

Unmatched invocations (ordered by similarity):

1 * codeProcessor.processMessage(Request@20b56ae3)
@Grab('org.spockframework:spock-core:0.7-groovy-2.0')
@Grab('cglib:cglib-nodep:3.1')

import spock.lang.*

class Test extends Specification {
   def "process code test"(){
        given:
            CodeProcessor codeProcessor =  Mock(CodeProcessor)
            Request request = new Request()
            request.setId(10)
        when:
            codeProcessor.processMessage(request)
        then:
            1 * codeProcessor.encodeMessage(request)
    }
}

class CodeProcessor {
   void processMessage(Request request) {
      encodeMessage(request)
   }
   void encodeMessage(Request request){} 
}

class Request {
    int id
}


Share : facebook icon twitter icon
PHP Mockery not returning mocked object from mocked function

PHP Mockery not returning mocked object from mocked function


By : Nitika Basil
Date : March 29 2020, 07:55 AM
this will help I'm setting up some unit tests for a class that uses Predis to connect to a Redis DB. I'm trying to mock the Client and Connection objects so that I can do the unit test. , Probably just need this:
code :
 $redis = m::mock("Predis\Client")
    ->shouldReceive( array( 'getConnection' => $redisConnection ) )
    ->once()
    ->andReturn('whatever you want to return?') // missing a return... by default it will return null.
    ->getMock();
Mocked private method is called instead of being mocked

Mocked private method is called instead of being mocked


By : Jon Grilli
Date : March 29 2020, 07:55 AM
it fixes the issue When you use PowerMockito.spy(new ExampleService()); all method calls at first will be delegated to instance of real class. What's why you get UnsupportedOperationException at line:
PowerMockito.when(exampleService, "privateMethod").thenReturn("");
code :
public class ExampleService {
    public String strangeMethod() {

        return privateMethod();
    }

    private String privateMethod() {
        return "b";
    }
}
@PrepareForTest(ExampleService.class)
@RunWith(PowerMockRunner.class)
public class TestPrivate {

    @Test
    public void strangeMethodTest() throws Exception {
        ExampleService exampleService = PowerMockito.spy(new ExampleService());
        PowerMockito.when(exampleService, "privateMethod").thenReturn("a");
        String actual = exampleService.strangeMethod();

        assertEquals("a",actual);
    }

}
Mocked function not getting called

Mocked function not getting called


By : Dima
Date : March 29 2020, 07:55 AM
seems to work fine I solved the issue. I was converting the collection object to array using toArray() function in my service. The mocked repository was expecting the collection but i was supplying the array. So I mocked the collection too and everything is working fine now.
Why does a mocked Class not enter a not mocked function?

Why does a mocked Class not enter a not mocked function?


By : user1538160
Date : March 29 2020, 07:55 AM
may help you . I do agree with memo, but still if you want to do It you can use real partial mocks https://static.javadoc.io/org.mockito/mockito-core/2.20.0/org/mockito/Mockito.html#16 for example:
code :
when(mock.someMethod()).thenCallRealMethod();
How do I create a mocked and a non-mocked instance of a single class within one test method?

How do I create a mocked and a non-mocked instance of a single class within one test method?


By : John Von Colln
Date : March 29 2020, 07:55 AM
will be helpful for those in need You can do it by adding a mockit.Invocation parameter and making the mock method reentrant. But it is simpler with the Expectations API, where you can declare an "@Injectable Entry expiredEntry" mock field/parameter which will not affect other instances of Entry.
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org