5

I have a problem with Gitlab CI/CD. I am making a Java project with Gradle. When I run my tests locally, they pass. However, when I push to my git repository, the test pipeline fails. Here is the screenshot of the error it gives me: Gitlab pipeline result

This is my local test. I am using Mockito testing in SpringBoot.

package com.its.backend;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.its.backend.entities.User;
import com.its.backend.repositories.UserRepository;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.junit.runner.RunWith;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {

@MockBean
private UserRepository repo;

@Test
void saveUserTest(){
    User user = new User("lala", "123");
    repo.save(user);
    when(repo.findByUsername(user.getUsername())).thenReturn(user);
    assertEquals("lala", repo.findByUsername(user.getUsername()).getUsername());
}
@Test 
void showAllUsersTest(){
    User user1 = new User("Ana", "321");
    repo.save(user1);
    User user2 = new User("Leo", "leo");
    repo.save(user2);
    List<User> users = new ArrayList<User>();
    users.add(user1);
    users.add(user2);
    when(repo.findAll()).thenReturn(users);
    assertEquals(2, users.size());
}
@Test
void findUserbyId(){
    User user = new User("Al", "277");
    repo.save(user);
    Optional<User> u = Optional.of(user);
    when(repo.findById(user.getId())).thenReturn(u);
    User userfound = u.get();
    assertEquals(user.getId(), userfound.getId()); 
}
@Test
void updateUser(){
    User user = new User("Seba", "bebe");
    repo.save(user);
    String newUsername = "Mai";
    user.setUsername(newUsername);
    repo.save(user);
    when(repo.findByUsername(user.getUsername())).thenReturn(user);
    assertEquals(newUsername, user.getUsername()); 
}
@Test
void deleteUser(){
    User user = new User("Kjala", "722");
    repo.save(user);
    when(repo.existsById(user.getId())).thenReturn(false);
    assertEquals(false, repo.existsById(user.getId()));
}
}  

My test stage in my .yml file looks like this:

test:
stage: test
allow_failure: true
script: gradle check
cache:
  key: "$CI_COMMIT_REF_NAME"
  policy: pull
  paths:
    - build
    - .gradle

Finally, these are the dependencies in my build.gradle: build.gradle dependencies

Any suggestions would help.

Ivona Bogovic
  • 59
  • 1
  • 2
  • You should probably pin the dependencies to specific versions, and maybe clean the Gradle dependency cache before running the tests. – tschumann Dec 10 '20 at 01:01
  • 3
    How did you solve this? – Robin Sep 29 '21 at 05:21
  • You declared this to be a SpringBootTest and you are using MockBean. This forces Spring to load the ApplicationContext which would not be necessary if you simply use MockitoExtension and Mock instead as described [here](https://stackoverflow.com/a/40962941/11934850). As far as I see, the test itself does not use anything from the ApplicationContext. – Sascha Doerdelmann Sep 14 '22 at 14:56

0 Answers0