小编Joc*_*ckX的帖子

JUnit5:指定多个@Nested类的执行顺序

Junit5 是否对@Nested同一父级中定义的类的执行顺序提供任何保证?通过我制作的一个简单示例,我注意到测试是以与声明相反的顺序执行的,但这种行为既没有记录(或者是吗?),也不与带@Test注释的方法默认的排序方式一致。

这也很烦人,因为显然我宁愿有一个非反向的顺序,如果不是一种类似于@TestMethodOrder.

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

class NestedOrderTest {
    @Nested
    class NestedTest1 {
        @Test
        void testSuccess1() {
            System.out.println(1);
        }
    }

    @Nested
    class NestedTest2 {
        @Test
        void testSuccess2() {
            System.out.println(2);
        }
    }

    @Nested
    class NestedTest3 {
        @Test
        void testSuccess3() {
            System.out.println(3);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

3
2
1
Run Code Online (Sandbox Code Playgroud)

java junit junit5

7
推荐指数
1
解决办法
3170
查看次数

Intellij 格式化程序链式方法调用

如何强制 IntellJ 代码格式化程序在与包装的链式方法调用不同的级别上自动缩进包装的参数列表:

编辑:有关更好的问题描述,请参阅更新的示例。如果我将每个连续的方法调用包装到一个新行,默认格式化程序会按预期工作。只有当我想每行留下一个或多个点时才会出现问题:

包装这个:

new Something()
    .chained("arg1", "arg2", "very long arg I want to see in new line")
    .chained("arg1", "arg2", "very long arg I want to see in new line")
    .extra().chained("arg1", "arg2", "very long arg I want to see in new line")
    .extra().chained("arg1", "arg2", "very long arg I want to see in new line");
Run Code Online (Sandbox Code Playgroud)

我希望是这样的:

new Something()
    .chained("arg1", "arg2", 
        "very long arg I want to see in new line")
    .chained("arg1", "arg2", 
        "very long arg I want to see …
Run Code Online (Sandbox Code Playgroud)

java code-formatting intellij-idea auto-indent

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

Spring Boot默认属性编码改变了吗?

我试图找到一种方法来为@ValueSpring Boot中的application.property文件中的注释访问的属性设置UTF-8编码.到目前为止,我已经通过创建bean成功地将编码设置为我自己的属性源:

@Bean
@Primary
public PropertySourcesPlaceholderConfigurer placeholderConfigurer(){
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setLocation(new ClassPathResource("app.properties");
    configurer.setFileEncoding("UTF-8");
    return configurer;
}
Run Code Online (Sandbox Code Playgroud)

这种解决方案存在两个问题 这一次,它不适用于Spring Boot默认使用的"application.properties"位置(http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config .html #boot-features-external-config),我被迫使用不同的文件名.

另一个问题是,通过它我可以手动定义和排序多个源的支持位置(例如,在jar与外部jar属性文件等中),从而重做已经完成的工作.

如何获取对已配置的PropertySourcesPlaceholderConfigurer的引用,并在应用程序初始化的恰当时间更改其文件编码?

编辑:也许我在其他地方犯了错误?这就是导致实际问题的原因:当我使用application.properties允许用户将个人名称应用于从应用程序发送的电子邮件时:

@Value("${mail.mailerAddress}")
private String mailerAddress;

@Value("${mail.mailerName}")
private String mailerName;                       // Actual property is ?wi?ty Miko?aj

private InternetAddress getSender(){
    InternetAddress sender = new InternetAddress();
    sender.setAddress(mailerAddress);
    try {
        sender.setPersonal(mailerName, "UTF-8"); // Result is Święty Mikołaj
        // OR: sender.setPersonal(mailerName);   // Result is ??wiÄ?ty Miko??aj
    } catch (UnsupportedEncodingException e) {
        logger.error("Unsupported encoding used in sender …
Run Code Online (Sandbox Code Playgroud)

java utf-8 properties-file spring-boot

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

在 Maven 中运行单个类时正确包含 jUnit5 @Nested 类

在 JUnit 5 中执行的类@Nested被命令在封闭类中的所有测试之后运行。如果我的目标是运行单个封闭类及其嵌套类,我如何使用 Maven 强制执行相同的行为?是否有命令行或 pom.xml 修改以使此示例测试通过?

package example;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

public class SomeTopLevelTest {

    private static boolean nestedDone = false;

    @Test
    void test1() {
        Assertions.assertFalse(nestedDone, 
            "Nested classes should execute after the enclosing tests");
    }

    @Nested
    class SomeNestedTest {
        @Test
        void test2() {
            nestedDone = true;
        }
    }

    @AfterAll
    static void recheck() {
        Assertions.assertTrue(nestedDone, "Nested class should be executed");
    }
}
Run Code Online (Sandbox Code Playgroud)

这确实在 IDE 中通过:
在此输入图像描述

但如果我尝试指定名称,则不在命令行中:

mvn test -Dtest=example.SomeTopLevelTest

[ERROR] Failures: 
[ERROR] …
Run Code Online (Sandbox Code Playgroud)

java maven maven-surefire-plugin junit5

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

C++枚举在一个类中 - 声明顺序很重要?

我正在使用visual studio 2010,并且不能完全搞定,这个类怎么错(语法错误:标识符'EnumType')并且不会编译:

class BrokenClassWithEnum
{
private:
    void useEnum (EnumType enumType); //syntax error : identifier 'EnumType '
public:
    enum EnumType 
    {
        VAL1,
        VAL2,
        VAL3
    };
}
Run Code Online (Sandbox Code Playgroud)

这没关系:

class WorkingClassWithEnum
{
public:
    enum EnumType 
    {
        VAL1,
        VAL2,
        VAL3
    };
private:
    void useEnum (EnumType enumType);
}
Run Code Online (Sandbox Code Playgroud)

班级范围发生了什么变化?

c++ visual-studio-2010

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

指向对象的全局指针会导致访问冲突?

也许你能为我澄清一些事情,因为我不知道我的想法到底哪里有缺陷。首先是一些代码:

说话者.h:

class talker       
{
    public:
    talker();
    void sayHello() {cout << "Hello";} ;
};
Run Code Online (Sandbox Code Playgroud)

另一个类.h:

class anotherClass
{
    public:
    anotherClass();
    void doSomethingYourself() { cout << "will do"; };
    void askHimToSayHello() { pointerToTalker->sayHello; };  
                          //Access violation, bad pointer(?)
};
Run Code Online (Sandbox Code Playgroud)

常见的.h:

 static talker *pointerToTalker;       
      // if I add here "= new talker", code works fine
Run Code Online (Sandbox Code Playgroud)

主.cpp:

#include "common.h"
int main()
{
    pointerToTalker = new talker;            // Here is the bug, but why?
    pointerToTalker -> sayHello;             // says Hello alright

    anotherClass *pointerToAnotherClass = …
Run Code Online (Sandbox Code Playgroud)

c++

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

搜索不同于"X"的第一个字符串的字符串

str.find('X')的反义词 - 在std :: string中找到与特定char不同的第一个字符的最有效方法是什么?如果我有一个主要由X'es组成的字符串,但在某些时候还有另一个字符 - 我如何快速找到它?

c++ stdstring

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