小编ZZZ*_*ZZZ的帖子

全局变量不能在 Jenkinsfile 的函数内部使用

我有这样的 Jenkinsfile:

def globalVariable = "my variable"

def myFunction() {
    def myString = "my String and ${globalVariable}
    .....
}

pipeline {
    ...
    < The globalVariable can be accessed inside the pipeline >  
}
Run Code Online (Sandbox Code Playgroud)

我收到运行时错误,抱怨无法在 myFunction 内部访问 globalVariable。

我认为 globalVariable 是全局的,可以从文件的任何地方访问。

我可以在函数内部引用它吗?

groovy jenkins jenkins-pipeline

10
推荐指数
1
解决办法
4573
查看次数

如何将 groupingBy 放入 Map 并更改键类型

我有一个代码,应该将交易对象列表分为两类;

public class Transaction {
    public String type;
    public Integer amount;
}
Run Code Online (Sandbox Code Playgroud)

以下函数通过检查条件将列表分为 2 类。流操作的输出映射是Map<Boolean, List<Transaction>>,但我想使用 String 作为其键。所以我手动转换它们。

public static Map<String, List<Transaction>> partitionTransactionArray(List<Transaction> t1) {
    Map<Boolean, List<Transaction>> result = list.stream().collect(Collectors.groupingBy(e -> ((e.type.equals("BUY") || e.type.equals("SELL")) && e.amount < 1000)));

    // I think this is not necessary
    Map<String, List<Transaction>> result1 = new HashMap<>();
    result1.put("APPROVED", result.get(true));
    result1.put("PENDING", result.get(false));

    return result1;
}
Run Code Online (Sandbox Code Playgroud)

但是,我认为必须有一种巧妙的方法可以让我在单个流操作中完成此操作。

有人可以帮忙吗?

编辑:

如果Map<String, List<Transactions>>我不想返回,而是希望Map<String, List<Integer>>列表仅包含交易金额,该怎么办?

我怎样才能在单个流操作中做到这一点?

java partitioning java-8 java-stream collectors

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

如何使用 Mockito 注解注入 @Value 构造函数参数

我有一堂课:

public class MyClass{
    MyClass(
            @Value("${my.protocol}") String protocol,
            @Value("${my.host}") String host,
            @Value("${my.port}") int port,
            @Autowired MyService myservice) {
        ........
    }

    ....

}
Run Code Online (Sandbox Code Playgroud)

然后我编写了一个使用 Mockito 的测试:

@ExtendWith(MockitoExtension.class)
class MyClassTest {

    @Mock
    private MyService myservice;

    @InjectMocks
    private MyClass myClass;

    ....
}
Run Code Online (Sandbox Code Playgroud)

测试失败了:

org.mockito.exceptions.base.MockitoException: 
Cannot instantiate @InjectMocks field named 'myClass'! Cause: the type 'MyClass ' has no default constructor
You haven't provided the instance at field declaration so I tried to construct the instance.
Examples of correct usage of @InjectMocks:
   @InjectMocks Service service …
Run Code Online (Sandbox Code Playgroud)

java junit mockito

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

运行 Rust 测试时,IntelliJ 会自动添加 -Z 不稳定选项

我尝试在 IntelliJ 中运行一个简单的 Rust 测试。

IntelliJ 使用的测试命令是:

cargo test --color=always --package myproject-rust --lib mycode::tests --no-fail-fast -- --format=json -Z unstable-options --show-output
Run Code Online (Sandbox Code Playgroud)

测试失败并出现以下错误:

error: the option `Z` is only accepted on the nightly compiler
Run Code Online (Sandbox Code Playgroud)

看来 IntelliJ 自动-Z unstable-options在命令中添加了 。

我使用测试函数左侧的绿色小播放按钮触发了测试:

在此输入图像描述

我的测试从命令行运行良好。

我无法-Z unstable-options从测试配置中删除它。

我可以做什么来删除它?

intellij-idea rust

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

如何在Java中捕获cucumber中的多行文本

我有一个 Java 黄瓜测试,其中包含以下测试用例:

.....
And the returned information should be
  """
  This is the first line of my text
  This is the second line of my text
  This is the third line of my text
  """
Run Code Online (Sandbox Code Playgroud)

我写的步骤def是:

@Then("^the returned information should be (.*)$")
Run Code Online (Sandbox Code Playgroud)

这不起作用,并且不会捕获多行文本。我该如何为此编写工作步骤定义?

java cucumber

3
推荐指数
1
解决办法
2993
查看次数

无法将 Rust 向量附加到另一个向量

我正在用 Rust 做 Leetcode 问题,遇到以下错误:

fn plus_one(digits: &mut Vec<u32>) -> &Vec<u32> {
    for i in (0.. digits.len()).rev() {
        if digits[i] < 10 {
            digits[i] = digits[i] + 1;
            break;
        }else{
            digits[i] = 0;
        }
    }

    if(digits[0] == 0) {
        let mut new_digits : Vec<u32> = vec![1];
        new_digits.extend(digits).        // This is the error
    }

    digits
}
Run Code Online (Sandbox Code Playgroud)

得到的错误:

--> src/easy/plus_one.rs:13:20
   |
13 |         new_digits.extend(digits)
   |                    ^^^^^^ the trait `Extend<&mut u32>` is not implemented for `Vec<u32>`
   |
   = help: the following other types …
Run Code Online (Sandbox Code Playgroud)

rust

3
推荐指数
1
解决办法
84
查看次数

解析逻辑表达式的正则表达式

我正在尝试使用正则表达式来解析带括号的逻辑表达式

例如:

((weight gt 10) OR (weight lt 100)) AND (length lt 50)
Run Code Online (Sandbox Code Playgroud)

我希望它可以解析为:

Group 1: (weight gt 10) OR (weight lt 100)
Group 2: AND
Group 3: length lt 50
Run Code Online (Sandbox Code Playgroud)

如果这个顺序改变:

(length lt 50) AND ((weight gt 10) OR (weight lt 100))
Run Code Online (Sandbox Code Playgroud)

我希望它可以解析为:

Group 1: length lt 50
Group 2: AND
Group 3: (weight gt 10) OR (weight lt 100)
Run Code Online (Sandbox Code Playgroud)

我试过的成本最高的是这个表达式:

(\((?>[^()]+|(?1))*\))
Run Code Online (Sandbox Code Playgroud)

问题在于它仅部分起作用:

((weight gt 10) OR (weight lt 100)) AND (length lt 50)

Group 1: ((weight gt 10) …
Run Code Online (Sandbox Code Playgroud)

regex

2
推荐指数
2
解决办法
165
查看次数

调用静态方法时未初始化静态 Java 字段

我有一个这样定义的类:

class MyClass {
    private static final String name = "<some string>";
    private static final String schema = "{<some json here>}";
    public static final MySchemaObject schemaObj = MyUtils.staticSchema(name, schema);   // Line1

    public static MySchemaObject getStaticSchema() {
        return schemaObj;
    }
}
Run Code Online (Sandbox Code Playgroud)

另一个类在静态初始化器中调用这个类:

class AnotherClass {
    private static final MySchemaObject schemaObject = MyClass.getStaticSchema();   <===== Always null
    ........
}
Run Code Online (Sandbox Code Playgroud)

在类中AnotherClass,静态变量schemaObject总是被初始化为空。这表明Line1在调用方法时未调用 MyClass 中的getStaticSchema

我的理解是在调用任何静态方法之前创建静态变量,但似乎并非如此。

有人可以帮我理解这一点吗?

编辑:MyUtils.staticSchema 永远不会返回 null;

java

0
推荐指数
1
解决办法
70
查看次数

如何将 Map&lt;String, String&gt; 转换为 String[],在键和值之间交替?

我试图找到一种方法来转换Map<String,String>String[]使用 Java 8 的方式。

一张地图

{"A":"a", "B","b", "C", "c"}` 
Run Code Online (Sandbox Code Playgroud)

应该转换为

["A", "a", "B", "b", "C", "c"] or ["B", "b", "C", "c", "A", "a"] ...
Run Code Online (Sandbox Code Playgroud)

对的顺序无关紧要,但键/值对必须在一起。

这样做的旧方法很明显:

Map<String, String> headers = .......
String[] array = new String[headers.size() * 2];
int i = 0;
for(Map.Entry<String, String> entry : headers.entrySet()) {
    array[i++] = entry.getKey();
    array[i++] = entry.getValue();
}
Run Code Online (Sandbox Code Playgroud)

java lambda

0
推荐指数
1
解决办法
53
查看次数

为什么 Java Set 上的 remove() 方法不起作用

我有以下代码:

    Set<Set<Integer>> groups = new HashSet<>();

    for (int[] edge : edges) {
        Set<Integer> match1 = null;
        Set<Integer> match2 = null;

        for(Set<Integer> group : groups) {
            if(group.contains(edge[0])) match1 = group;
            if(group.contains(edge[1])) match2 = group;
        }

        if(match1 != null && match1 == match2) {
            result = edge;
        }else if(match1 != null && match2 != null && match1 != match2) {
            match1.addAll(match2);
            groups.remove(match2);     <---- This does not remove match2 from set
        }else{
            Set<Integer> newGroup = new HashSet<>();
            newGroup.add(edge[0]);
            newGroup.add(edge[1]);
            groups.add(newGroup);
        }
        
        .........
    } …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
61
查看次数