小编ihe*_*heb的帖子

MapStruct:模拟嵌套映射器

我使用 MapStruct 来映射我的实体,并且我正在使用 Mockito 模拟我的对象。

我想测试一个包含 mapStruct 映射的方法。问题是嵌套映射器在我的单元测试中始终为空(在应用程序中运行良好)

这是我的映射器声明:

@Mapper(componentModel = "spring", uses = MappingUtils.class)
public interface MappingDef {
     UserDto userToUserDto(User user)
}
Run Code Online (Sandbox Code Playgroud)

这是我的嵌套映射器

@Mapper(componentModel = "spring")
public interface MappingUtils {
    //.... other mapping methods used by userToUserDto
Run Code Online (Sandbox Code Playgroud)

这是我要测试的方法:

@Service
public class SomeClass{
        @Autowired
        private MappingDef mappingDef;

        public UserDto myMethodToTest(){

        // doing some business logic here returning a user
        // User user = Some Business Logic

        return mappingDef.userToUserDto(user)
}
Run Code Online (Sandbox Code Playgroud)

这是我的单元测试:

@RunWith(MockitoJUnitRunner.class)
public class NoteServiceTest {

    @InjectMocks
    private SomeClass someClass;
    @Spy …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mockito mapstruct

7
推荐指数
3
解决办法
8916
查看次数

Primefaces DataExporter设置表格列宽度

我有一个Primefaces DataGrid,我导出了Primefaces DataExporter,但我无法弄清楚如何调整列的大小.

我添加了一个preorcessor

<p:dataExporter type="pdf" target="tbl" fileName="cars" preProcessor="#{customizedDocumentsView.preProcessPDF}" />
Run Code Online (Sandbox Code Playgroud)

这是我的bean中的代码

public void preProcessPDF(Object document) {
        Document pdf = (Document) document;
        pdf.open();
        pdf.setPageSize(PageSize.A4);

        //I need to do something like that
        //PdfPTable table = new PdfPTable(4);
        //float[] columnWidths = new float[] {10f, 20f, 30f, 10f};
        //table.setWidths(columnWidths);
    } 
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

pdf-generation data-export primefaces

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

StyleCop.Analyzers 支持 C# 记录吗?

问题描述:

C#在使用 的项目中添加记录时StyleCop.Analyzers,会显示警告:

Warning AD0001  Analyzer 'StyleCop.Analyzers.DocumentationRules.SA1649FileNameMustMatchTypeName' threw an exception of type 'System.ArgumentException' with message 'Unhandled declaration kind: RecordDeclaration'.    

System.ArgumentException: Unhandled declaration kind: RecordDeclaration
       at StyleCop.Analyzers.Helpers.NamedTypeHelpers.GetNameOrIdentifier(MemberDeclarationSyntax member)
       at StyleCop.Analyzers.Helpers.FileNameHelpers.GetConventionalFileName(MemberDeclarationSyntax declaration, FileNamingConvention convention)
       at StyleCop.Analyzers.DocumentationRules.SA1649FileNameMustMatchTypeName.Analyzer.HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCopSettings settings)
       at StyleCop.Analyzers.AnalyzerExtensions.<>c__DisplayClass0_0.<RegisterSyntaxTreeAction>b__0(SyntaxTreeAnalysisContext c)
       at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteSyntaxTreeActionsCore>b__59_1(ValueTuple`2 data)
       at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
Run Code Online (Sandbox Code Playgroud)

这是一个错误吗StyleCop.Analyzers

重现步骤:

  • 创建一个新的.Net6控制台项目(甚至.Net5)。

  • C#在其中添加一条记录:public record Member(string FirstName, string LastName);

  • 安装包StyleCop.Analyzers v1.1.118

  • 查看 Visual Studio 错误列表,您将看到警告。

c# stylecop record

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