selim
selim

Reputation: 93

JUnit test for my method

Hello every one I need to write unit tests for my methods. I'm having a bit of trouble because I'm new to JUnit. I need to write a test for this method. this is my method

@Override
public Long countSellingOrdersInQueue(String principal) {
    List<String> states = Arrays.asList(PENDING.name(), REGULARIZED.name());
    return orderRepository.countByArticleUserIdAndStateIn(principal, states);
}

I try but i'm blocked and this is my result

P.S. test is passed but I don't understand if my test is true

@MockBean
private OrderRepository orderRepository;

private String principal ;

@Test
public void countSellingOrdersInQueueTest(){
    orderService.countSellingOrdersInQueue(principal);
    List<String> states = Arrays.asList(PENDING.name(), REGULARIZED.name());
    orderRepository.countByUserIdAndStateIn(principal,states);
}

Upvotes: 0

Views: 97

Answers (2)

Thiru
Thiru

Reputation: 2709

In your case, it is just the unit test, you need not use @MockBean, as it loads the context. Unit tests are meant to be run faster, using @MockBean, will load the context and takes time to complete the test. Here is the suggestion of when to use @Mock and when to use @MockBean.

As Maxim said, there were no assertions in the test. That was the reason why the tests weren't failing.

Few things to keep in mind while writing the test.

  • Tests are considered as documentation for the code, it should be more readable in such a way it makes others to understand the code.
  • As said before, unit tests are for giving faster feedback
  • Have AAA (Arrange, Act, Assert) structure in tests. More info here

Here is the code:

public class OrderServiceTest {

    @InjectMocks
    private OrderService orderService;

    @Mock
    private OrderRepository orderRepository;

    @Before
    public void setUp() throws Exception {
        initMocks(this);
    }

    @Test
    public void countSellingOrdersInQueueTest(){
        when(orderRepository.countByArticleUserIdAndStateIn(any(), any())).thenReturn(1L);
        String principal = "dummyString";

        Long actualCount = orderService.countSellingOrdersInQueue(principal);

        List<String> expectedStates = Arrays.asList("State 1", "State 2");
        assertThat(actualCount, is(1L));
        verify(orderRepository).countByArticleUserIdAndStateIn(principal, expectedStates);
    }
}

Upvotes: 3

Maxim Kasyanov
Maxim Kasyanov

Reputation: 1058

Test passes because you don't have any assertion, which checks result. You just invoke methods which executes without exception.

Simple test example:

    @Test
    public void test() {
        assertEquals(true, true);
    }

In your case test will be look likes:

    @Test
    public void countSellingOrdersInQueueTest(){
        orderService.countSellingOrdersInQueue(principal);
        List<String> states = Arrays.asList(PENDING.name(), REGULARIZED.name());
        orderRepository.countByUserIdAndStateIn(principal,states);
        assertEquals(10, orderRepository.countByUserIdAndStateIn(principal,states));//10 replace to expectetion count
        //add some more checks
    }

Upvotes: 2

Related Questions