Skip to content

Instantly share code, notes, and snippets.

@lofidewanto
Last active October 28, 2020 20:50
Show Gist options
  • Save lofidewanto/2efb9f04e1aceb5a19ad7e275775461c to your computer and use it in GitHub Desktop.
Save lofidewanto/2efb9f04e1aceb5a19ad7e275775461c to your computer and use it in GitHub Desktop.
IndexedDB with Patterns - Unit Test with JUnit and Mockito
package com.github.lofi.client;
import static org.mockito.Mockito.doReturn;
...
import org.jboss.elemento.InputBuilder;
...
@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
class ProductCompositeTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Product product;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private InputBuilder<HTMLInputElement> input;
private ProductComposite productComposite;
@BeforeEach
void setUp(@Mock(answer = Answers.RETURNS_DEEP_STUBS) ProductService productService) throws Exception {
productComposite = spy(new ProductComposite(productService));
when(productService.createProduct()).thenReturn(product);
}
@Test
void on_ProductCreated_amount_bigger_than_350() {
when(product.getCalculatedPriceWithAmount()).thenReturn(500);
doReturn(input).when(productComposite).renderInputElements();
productComposite.onProductCreated();
verify(productComposite, times(1)).renderInputElements();
verify(input, times(1)).element();
}
@Test
void on_ProductCreated_amount_smaller_than_350() {
when(product.getCalculatedPriceWithAmount()).thenReturn(200);
productComposite.onProductCreated();
verify(productComposite, times(0)).renderInputElements();
verify(input, times(0)).element();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment