Asserting with mockito Spring WebFlux

While writing some webflux code, I had some issues with mockito to verify interactions

Note here, that mokito.verify worked only after calling StepVerifier.verify() which blocks until the code completes

        Mono<ProcessedCharge> checkoutMono = checkoutService.checkout(cartId, "bla");
//        verify(cartService).getCartWithDetails(cartId);
//        verify(itemInventoryService).updateItemsStock(anyMap());
//        verify(cartService).deleteCart(cartId);

        StepVerifier
                .create(checkoutMono)
                .expectNextCount(1L)
                .expectComplete()
                .verify();
        /*
        I think the code under test runs asynchronously,
        So verifications work only if I moved the code
        after StepVerifier.verify() - which blocks and waits for the code to execute/complete
        * */

        verify(cartService).getCartWithDetails(cartId);
        verify(itemInventoryService).checkOrderInStock(any());
        verify(itemInventoryService).updateItemsStock(anyMap());
        verify(cartService).deleteCart(cartId);

Mockito Spy, Stubbing a Spy

https://www.baeldung.com/mockito-spy

Wrap an instance of a real business object in a mockito spy, so that you can verify certain behavior was called

SubscriptionEntity entity = Mockito.spy(subscriptionEntity());

No, on the spy you can assert that a certain method was called

verify(entity, times(1)).setFcaSalesForceToken(FCA_SFO_TOKEN);
        verify(repository, times(1)).saveSubscription(entity);

You can go even further and you can override existing behavior on a real business object, to customize it according to your needs during the testing: see here: https://www.baeldung.com/mockito-spy#stubbing-a-spy

@Test
public void whenStubASpy_thenStubbed() {
    List<String> list = new ArrayList<String>();
    List<String> spyList = Mockito.spy(list);

    assertEquals(0, spyList.size());

    Mockito.doReturn(100).when(spyList).size();
    assertEquals(100, spyList.size());
}

In Spring you can Spy a Bean/Service like

    @Autowired
    @SpyBean
    PriceService priceService;

Customizing Jaxb generation plugin

Jaxb generation plugin

https://stackoverflow.com/a/33872554/1864614

https://www.javaer101.com/en/article/1278220.html
>

Ema P. :

To fix this issue, you need to create bind.xjb file. Create the bind.xjb file in the path src/main/java/bindings. In this file we will solve the conflicts that prevent the creation of ObjectFactory. What is happining is that in you schema you have the ObjectFactory declared in schema6 and schema5. So a workaround to this would that this schema to be generated in separated packages. In the bind.xjb file, we will instruct the maven plugin to generate the classes from schema5 and schema6 in different packages. Below it is the code for bind.xjb file:

<jaxb:bindings version="1.0"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc">

    <jaxb:bindings
        schemaLocation="schema6.xsd"
        node="/xs:schema">

        <jaxb:schemaBindings>
            <jaxb:package name="bankup.schema6" />
        </jaxb:schemaBindings>

    </jaxb:bindings>

    <jaxb:bindings
        schemaLocation="schema5.xsd"
        node="/xs:schema">

        <jaxb:schemaBindings>
            <jaxb:package name="bankup.schema5" />
        </jaxb:schemaBindings>

    </jaxb:bindings>

</jaxb:bindings>
<plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <executions>

                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>

                        <configuration>
                            <bindingDirectory>${project.basedir}/src/main/resources/bindings</bindingDirectory>
                            <bindingIncludes>
                                <include>bind.xjb</include>
                            </bindingIncludes>
                            <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory>
                            <schemaIncludes>
                                <include>*.wsdl</include>
                            </schemaIncludes>
                            <clearOutputDir>true</clearOutputDir>
                            <vmArgs>
                                <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
                            </vmArgs>
                        </configuration>

                    </execution>


                </executions>

            </plugin>