How with groovy mock pulsar producer if I has code:
myProducer.newMessage().key(String.valueOf(1L)).value(nameBytes).send()
my producer is mock myProducer = Mock(Producer). But in the test I got NullPointerException
I found out, should be:
TypedMessageBuilder messageBuilder = Mock()
myProducer.newMessage() >> messageBuilder
messageBuilder.key(*_) >> messageBuilder
messageBuilder.value(*_) >> messageBuilder
Related
I am currently trying to use spock instead of junit in my unit tests.
But I ran into problem of MissingMethodInvocationException.
I am mocking Provider<T>.get(), like below
Provider<SomeOjb> a = Mock()
def setup(){
SomeOjb obj = new SomeObj();
Mockito.when(a.get()).thenReturn(obj)
}
but after running it I am getting
org.mockito.exceptions.misusing.MissingMethodInvocationException
when() requires as argument which has to be 'a method call on a mock'
I tried two diffrent approach, one of them is to create mock as
def a = Mock(Provider<SomeObj>) but in this case I getting syntax error after running, issue is with <>
And the last idea was to just use annotation
#Mock
Provider<SomeObj> a;
and use when/then as before, and in this case it worked alright.
Any idea why firt idea is not correct?
Obviously I am stupid.
Issue was because I used Mockito api. Not spock mock api.
It should looks like:
Provider<SomeOjb> a = Mock()
def setup(){
SomeOjb obj = new SomeObj();
a.get() >> obj
}
I'm trying to figure out how to inject a mock in my test, using Rails 5.1 and minitest. I'm testing this method of the following class ...
module WebsocketClient
class Proxy
...
def connect
#websocket = WebSocket::Client::Simple.connect #websocket_url
I would like to have the line
WebSocket::Client::Simple.connect #websocket_url
return a mock instead of actually trying to connect, so I tried doing this in my unit test
test "connect with open" do
my_worker = my_workers(:one)
assert my_worker.websocket_url.present?, "A pre-condition of this test is that the stratum worker have a websocket URL defined."
proxy = WebsocketClient::Proxy.new( my_worker )
# Stub call to connect
mock_ws = MiniTest::Mock.new
WebSocket::Client::Simple.stub(:connect, my_worker.websocket_url) do
mock_ws
end
# Call the connect method
ws_client = WebsocketClient::Proxy.new(my_worker)
ws_client.connect
However, upon running my test, the mock is not getting executed. Instead the "WebSocket::Client::Simple.connect #websocket_url" is being called as normal. How do I inject my mock in my test?
In Grails 3.2.x and earlier I could do something like this in a spock unit test:
def myServiceMock = Mock(MyService) {
someMethod() >> 42
}
Closure doWithSpring() {{ ->
myService(InstanceFactoryBean, myServiceMock, MyService)
}}
def "some test"(){
expect:
service.myService.someMethod() == 42
}
This would enable the mock to be injected in collaborating classes.
See: http://docs.grails.org/3.2.4/guide/testing.html
Under the "doWithSpring and doWithConfig callback methods, FreshRuntime annotation" section.
In Grails 3.3.2 it does not seem to work anymore.
And the mention of it has been removed from the testing documentation.
Is there any way to accomplished that behaviour again?
Many thanks in advance!
/brian
Grails 3.3 comes with new testing framework.
Here you can find docs - https://testing.grails.org/latest/guide/index.html
To run test on grails 3.3. you can modify your code in this way:
def myServiceMock = Mock(MyService) {
someMethod() >> 42
}
def setup() {
defineBeans{
myService(InstanceFactoryBean, myServiceMock, MyService)
}
}
In Grails 3.1.12, I want to unit test a service:
#Transactional
class PlanService {
List<Plan> getPlans(Map params) {
def currentUser = (User)springSecurityService.getCurrentUser()
return Plan.findAllByCompany(currentUser.employer, params)
}
}
Like this:
#TestFor(PlanService)
#Mock([Plan, User, Company])
class PlanServiceSpec extends Specification {
void "Retrieve plan from the current user"() {
setup:
// create and save entities here
when: "the plans are retrieved"
def params = null
def plans = service.getPlans(params)
then: "the result should only include plans associated to the current user's company"
plans.size() == 2
}
Running the test from the console:
grails> test-app my.PlanServiceSpec -unit
Fails with:
my.FundingPlanServiceSpec > Retrieve plan from the current user FAILED
java.lang.IllegalStateException at PlanServiceSpec.groovy:48
and in the test report (HTML):
java.lang.IllegalStateException: No transactionManager was specified.
Using #Transactional or #Rollback requires a valid configured transaction manager.
If you are running in a unit test ensure the test has been properly configured
and that you run the test suite not an individual test method.
Now if I comment out the #Transactional annotation in the service, the test passes, but that's not the intended implementation. I am able to work around the problem by mocking the transaction manager:
service.transactionManager = Mock(PlatformTransactionManager) {
getTransaction(_) >> Mock(TransactionStatus)
}
But this seems very awkward, if not wrong.
Is there some incantation I forgot to invoke?
EDIT: looks similar to an old bug, but it's been closed more than a year.
Have you tried what a comments says that fixes the problem? If not, try to annotate the test class with:
#TestMixin(DomainClassUnitTestMixin)
and then:
service.transactionManager = getTransactionManager()
Was getting the same error in grails 3.3.2 when trying to test transactional service.
adding DataTest interface solved the issue for me.
class HelloServiceSpec extends Specification implements ServiceUnitTest<HelloService>, DataTest {
}
I am mocking the socket class and ObjectInputstream class.
Here is a test case which is giving me
java.io.StreamCorruptedException
The test case is as follows:
public void test_tryPush() throws IOException {
ByteArrayInputStream inside = new ByteArrayInputStream("hey".getBytes());
Mockito.when(socket.getInputStream()).thenReturn(inside);
input = new ObjectInputStream(socket.getInputStream());
assertSame(inside, socket.getInputStream());
}
If I do not delcare the input object of ObjectInputStream class the test passes.
But the test above gives the following trace.
Running tests
Test running started
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2109)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:372)
at tests.MockingTest.test_tryPush(MockingTest.java:113)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
Finish
Please can anybody tell me what is going wrong here.
I am trying to mock the client side socket.