英文:
How to test when bean is using a custom Lombok Builder
问题
以下是翻译好的内容:
下面的代码在使用带有自定义构建器的Lombok Builder注解的情况下工作。
当我想要使用值创建此Bean时,我遇到了测试问题。
我不能再使用Setter方法(故意这样设计),也不能仅使用.builder()
,因为有自定义构建器存在。
我的问题是,我如何在测试中创建此Bean?请注意,我确实想使用构建器,而不是使用Lombok的@Value
注解。自定义构建器对于与Jackson一起使用是必要的。
我能想到的一个可能性是,在自定义构建器内部添加一个构造函数,但希望出于测试目的不必这样做。
请给予建议。谢谢。
工作中的Bean设置。
@Getter
@JsonDeserialize(builder = MyData.MyDataBuilder.class)
@Builder(builderClassName = "MyDataBuilder", toBuilder = true)
public class MyData {
@JsonProperty("some_key")
private String skey;
@JsonProperty("name")
private String name;
// needed to work with Jackson
@JsonPOJOBuilder(withPrefix = "")
static class MyDataBuilder {}
}
尝试创建对象的测试。如上所述,以下方式都不会起作用。
MyData.builder()
.skey("12345")
.name("some_name")
.build();
或者
MyData myData = new MyData();
myData.skey("12345");
myData.name("some_name");
英文:
The following code works where my bean is using Lombok Builder annotations with a custom builder.
I am having issues with testing when I want to create this bean with values.
I can no longer use Setters (intentional) and can't just use .builder() due to the custom builder.
My question is, how do I create this bean in my tests? Note that I do want to use a builder and not looking to use Lombok's @Value annotation. The custom builder is necessary to work with Jackson.
One possibility I can think of is to add a constructor inside the custom builder which I hopefully don't need to do just for the sake of testing.
Please advice. Thanks.
Working Bean setup.
@Getter
@JsonDeserialize(builder = MyData.MyDataBuilder.class)
@Builder(builderClassName = "MyDataBuilder", toBuilder = true)
public class MyData {
@JsonProperty("some_key")
private String skey;
@JsonProperty("name")
private String name;
// needed to work with Jackson
@JsonPOJOBuilder(withPrefix = "")
static class MyDataBuilder {}
}
Test trying to create the Object. Following won't work as mentioned above.
MyData.builder()
.skey("12345")
.name("some_name")
.build();
or
MyData myData = new MyData();
myData.skey("12345");
myData.name("some_name");
答案1
得分: 0
你可以自定义构建器类的访问级别。如果您不自定义,Lombok会将其默认设置为public
。
如果类本身和至少一个构造函数是public
的,就可以从任何地方创建实例(不仅限于同一包)。但在大多数情况下,如果构建器模式在其所在的包中是有益的,那么在其他地方使用时也应当将构建器类设为public
。
如果您不希望其他包中的类被实例化,那么构建器就不应该是public
的(构造函数也是如此)。然而,这样一来,您的测试可能也有些缺陷,或者放错了包。
所以,要么将自定义的构建器类设为public
,要么修改您的测试。
英文:
You can customize the builder class to have any access level you want. Lombok makes it public
by default if you don't customize it.
If the class itself and at least one constructor are public
, it is possible to create instances from everywhere (not just the same package). But then there is no reason in most cases why the builder class should not be public
, too: If the builder pattern is beneficial in its package, it will probably also be beneficial everywhere else.
If you don't want your class to be instantiated in other packages, then the builder should not be public
(and also no constructor). However, then your test is also somehow flawed or in the wrong package.
So either make the custom builder class public
, or change your test.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论