java—测试在本地传递,但在git管道中失败

rnmwe5a2  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(285)

我对gitlab ci/cd有问题。我正在和gradle做一个java项目。当我在本地运行测试时,它们通过了。但是,当我推送到git存储库时,测试管道失败了。下面是它给我的错误截图:gitlab pipeline result
这是我的本地测试。我在springboot中使用mockito测试。

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()));
}
}

.yml文件中的测试阶段如下所示:

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

最后,这些是我的build.gradle中的依赖项:build.gradle依赖项
任何建议都会有帮助。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题