小编Ren*_*ibe的帖子

Spock - 使用数据表测试异常

如何用Spock以一种很好的方式测试异常(例如数据表)?

示例:具有validateUser可以使用不同消息抛出异常的方法,或者如果用户有效则无异常.

规范类本身:

class User { String userName }

class SomeSpec extends spock.lang.Specification {

    ...tests go here...

    private validateUser(User user) {
        if (!user) throw new Exception ('no user')
        if (!user.userName) throw new Exception ('no userName')
    }
}
Run Code Online (Sandbox Code Playgroud)

变式1

这一个是工作,但真正的意图是所有的混乱时,/然后标签和的多次呼吁validateUser(user).

    def 'validate user - the long way - working but not nice'() {
        when:
        def user = new User(userName: 'tester')
        validateUser(user)

        then:
        noExceptionThrown()

        when:
        user = new User(userName: null)
        validateUser(user)

        then:
        def ex …
Run Code Online (Sandbox Code Playgroud)

testing groovy exception spock

47
推荐指数
5
解决办法
2万
查看次数

如何使用声明性Jenkins管道在同一节点上运行多个阶段?

目标
在同一节点上运行声明性Jenkins管道的多个阶段.

设置
这只是一个显示问题的最小示例.有2个Windows节点"windows-slave1"和"windows-slave2"都标有"windows"标签.

注意:我真正的Jenkinsfile不能使用全局代理,因为有一些阶段需要在不同的节点上运行(例如Windows与Linux).

预期行为
Jenkins根据标签选择"Stage 1"中的一个节点,并在"Stage 2"中使用相同的节点,因为变量windowsNode已更新为"Stage 1"中选择的节点.

实际行为
"阶段2"有时在与"阶段1"相同的节点上运行,有时在不同的节点上运行.请参阅下面的输出.

Jenkinsfile

#!groovy

windowsNode = 'windows'

pipeline {
  agent none
  stages {
    stage('Stage 1') {
      agent {
        label windowsNode
      }
      steps {
        script {
          // all subsequent steps should be run on the same windows node
          windowsNode = NODE_NAME
        }
        echo "windowsNode: $windowsNode, NODE_NAME: $NODE_NAME"
      }
    }
    stage('Stage 2') {
      agent {
        label windowsNode
      }
      steps {
        echo "windowsNode: $windowsNode, NODE_NAME: $NODE_NAME"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

产量 …

jenkins jenkins-pipeline

26
推荐指数
3
解决办法
2万
查看次数

模拟弹簧组件

我正在使用 Mockito 来模拟春豆。

当我模拟接口时它工作正常。

在我们的应用程序中,很少有@Component bean 没有实现任何接口。

当我尝试模拟此类组件时,spring 上下文会尝试在这些组件中注入属性。

Mockito 不支持模拟没有实现任何接口的弹簧组件吗?

根据要求附上示例

public interface EmployeeInterface {
    public Long saveEmployee(Employee employee);
}

@Component
public class EmployeeImpl implements EmployeeInterface {

    @Autowired
    public EmailSender emailSender

    public Long saveEmployee(Employee employee) {
        ...
    }
}

public interface EmailSender {
    public boolean sendEmail(Email email);
}

@Component
public class EmailSenderImpl implements EmailSender {

    @Autowired
    MailServerInfo  MailServerInfo;

    public boolean sendEmail(Email email) {
        ...
    }
}

public interface MailServerInfo {
    public String getMailServerDetails();
}

@Component
public class MailServerInfoImpl { …
Run Code Online (Sandbox Code Playgroud)

mocking spring-test mockito powermock powermockito

5
推荐指数
1
解决办法
1万
查看次数

MockMVC 对异步服务执行后期测试

我需要测试一个调用异步服务的控制器。

控制器代码

@RequestMapping(value = "/path", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Result> massiveImport(HttpServletRequest request) {
    try {
        service.asyncMethod(request);
    } catch (Exception e) {
        e.printStackTrace();
        return new ResponseEntity<>(new Result(e.getMessage()), HttpStatus.BAD_REQUEST);
    }

    return new ResponseEntity<>(new Result(saveContact.toString()), HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)

服务代码

@Async
public Future<Integer> asyncMethod(HttpServletRequest request) throws IllegalFieldValueException, Exception {
    ...
    return new AsyncResult<>(value);
}
Run Code Online (Sandbox Code Playgroud)

测试代码

MvcResult result = getMvc().perform(MockMvcRequestBuilders.fileUpload("/path/")
                           .header("X-Auth-Token", accessToken)
                           .accept(MediaType.APPLICATION_JSON))
                           .andDo(print())
                           .andReturn();
Run Code Online (Sandbox Code Playgroud)

测试没问题。但是我会在关闭测试之前等待完成异步服务。

有没有办法做到这一点?

asynchronous mocking spring-mvc mockito async-await

5
推荐指数
1
解决办法
7298
查看次数