参数化测试因mvn测试而失败

时间:2019-11-27 15:41:34

标签: java maven junit lombok parameterized

我在一个简单的参数化Java测试中苦苦挣扎,找不到合适的解决方案:-S

也许您可以提示我错过了什么。

以下是我一直在测试的场景 与

一起运行
  • intellij->没有错误,所有测试均通过
  • mvn clean -Dtest:KnockoutTests test->没有错误,所有测试均通过
  • mvn clean test(在本地和jenkins上)-> ArrayIndexOutOfBoundsException

failing set initialisation

当我在类hashCode中复制完全生成的RoundDefinition方法时,它可以正常工作。但是我不想用确切生成的方法显式地覆盖hashCode方法……(显然这不能成为解决方案)

模型

@Entity
@Data
@Table( name = "tbl_rounddefinition" )
@JsonIgnoreProperties( value = { "hibernateLazyInitializer", "handler", }, ignoreUnknown = true )
@ToString( exclude = { "tournament" } )
@EqualsAndHashCode( exclude = { "tournament" } )
public class RoundDefinition implements Serializable {

    @Id
    @GeneratedValue( strategy = IDENTITY )
    @Column( name = "rounddefinition_id" )
    private Long id;

    @ManyToOne( fetch = FetchType.LAZY )
    @JoinColumn( name = "rounddefinition_linktournamentid" )
    @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id" )
    @JsonIdentityReference( alwaysAsId = true )
    private Tournament tournament;

    @Column( name = "rounddefinition_round" )
    private Integer round;

    @Column( name = "rounddefinition_numberOfRoundresults" )
    private Integer numberOfRoundresults;
}
@Data
@ToString( exclude = { [...] } )
@EqualsAndHashCode( exclude = { [...], "roundDefinitions" } )
@Entity
@Table( name = "tbl_tournament" )
@JsonIgnoreProperties( value = { "hibernateLazyInitializer", "handler" }, ignoreUnknown = true )
public class Tournament implements Serializable {

[...]

    @OneToMany( fetch = FetchType.LAZY, mappedBy = "tournament", cascade = CascadeType.ALL, orphanRemoval = true )
    @JsonSerialize( using = CollectionSerializer.class )
    @JsonDeserialize( using = CollectionDeserializer.class )
    @OrderBy( " round ASC " )
    private Set<RoundDefinition> roundDefinitions = new HashSet<>();

[...]

}

测试

@RunWith( Parameterized.class )
@Slf4j
public class KnockoutTests {

    @InjectMocks
    private Knockout underTest;

    @Mock
    private MatchRepository matchRepository;

    private Integer tournamentSize;
    private Integer expectedMatchSize;
    private Integer expectedMaxRound;
    private List<RoundDefinition> roundDefinitions;

    public KnockoutTests( Integer tournamentSize, Integer expectedMatchSize, Integer expectedMaxRound,
            List<RoundDefinition> roundDefinitions )
    {
        this.tournamentSize = tournamentSize;
        this.expectedMatchSize = expectedMatchSize;
        this.expectedMaxRound = expectedMaxRound;
        this.roundDefinitions = roundDefinitions;
    }

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks( this );
    }

    @Test
    public void testGenerate() {
        Tournament tournament = new Tournament();
        tournament.setTeamsize( 1 );
        System.out.println( "############# " + roundDefinitions.toString() + " #############" );
        tournament.setRoundDefinitions( new HashSet<>( roundDefinitions ) ); // <<-- Here I'm getting this weird ArrayIndexOutOfBoundsExceptions
        [...]
    }

    @Parameterized.Parameters( name = "{index}: Test with tournamentSize={0}, expectedMatchSize: {1}, expectedMaxRound: {2}" )
    public static Collection testData() {
        return Arrays.asList( new Object[][] {
                { 1, 1, 1, new ArrayList<RoundDefinition>() {{
                    add( getRoundDefinition( 1L, 1, 1 ) );
                }} },
                { 2, 1, 1, new ArrayList<RoundDefinition>() {{
                    add( getRoundDefinition( 1L, 1, 1 ) );
                }} },
                { 15, 15, 4, new ArrayList<RoundDefinition>() {{
                    add( getRoundDefinition( 1L, 1, 1 ) );
                    add( getRoundDefinition( 2L, 3, 3 ) );
                    add( getRoundDefinition( 3L, 4, 5 ) );
                }} },
                { 16, 15, 4, new ArrayList<RoundDefinition>() {{
                    add( getRoundDefinition( 1L, 1, 1 ) );
                }} },
                { 17, 31, 5, new ArrayList<RoundDefinition>() {{
                    add( getRoundDefinition( 1L, 1, 1 ) );
                }} },
                { 129, 255, 8, new ArrayList<RoundDefinition>() {{
                    add( getRoundDefinition( 1L, 1, 1 ) );
                }} },
                { 256, 255, 8, new ArrayList<RoundDefinition>() {{
                    add( getRoundDefinition( 1L, 1, 1 ) );
                }} },
                { 257, 511, 9, new ArrayList<RoundDefinition>() {{
                    add( getRoundDefinition( 1L, 1, 1 ) );
                    add( getRoundDefinition( 2L, 3, 3 ) );
                    add( getRoundDefinition( 3L, 4, 5 ) );
                }} }
        } );
    }

    private static RoundDefinition getRoundDefinition( long id, int round, int numberOfRoundresults ) {
        RoundDefinition rd = new RoundDefinition();
        rd.setId( id );
        rd.setRound( round );
        rd.setNumberOfRoundresults( numberOfRoundresults );
        return rd;
    }
[...]
}

控制台输出

############# [RoundDefinition(id=1, tournament=null, round=1, numberOfRoundresults=1)] #############

############# [RoundDefinition(id=1, tournament=null, round=1, numberOfRoundresults=1), RoundDefinition(id=2, tournament=null, round=3, numberOfRoundresults=3), RoundDefinition(id=3, tournament=null, round=4, numberOfRoundresults=5)] #############
############# [RoundDefinition(id=1, tournament=null, round=1, numberOfRoundresults=1)] #############
############# [RoundDefinition(id=1, tournament=null, round=1, numberOfRoundresults=1)] #############
############# [RoundDefinition(id=1, tournament=null, round=1, numberOfRoundresults=1)] #############
############# [RoundDefinition(id=1, tournament=null, round=1, numberOfRoundresults=1)] #############
############# [RoundDefinition(id=1, tournament=null, round=1, numberOfRoundresults=1), RoundDefinition(id=2, tournament=null, round=3, numberOfRoundresults=3), RoundDefinition(id=3, tournament=null, round=4, numberOfRoundresults=5)] #############
Tests run: 8, Failures: 0, Errors: 8, Skipped: 0, Time elapsed: 0.015 sec <<< FAILURE! - in pkg.service.tournament.handler.generator.KnockoutTests
testGenerate[0: Test with tournamentSize=1, expectedMatchSize: 1, expectedMaxRound: 1](pkg.service.tournament.handler.generator.KnockoutTests)  Time elapsed: 0.008 sec  <<< ERROR!
java.lang.ArrayIndexOutOfBoundsException: 35
    at pkg.model.entities.RoundDefinition.hashCode(RoundDefinition.java:26)
    at java.util.HashMap.hash(HashMap.java:339)
    at java.util.HashMap.put(HashMap.java:612)
    at java.util.HashSet.add(HashSet.java:220)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
    at java.util.HashSet.<init>(HashSet.java:120)
    at pkg.service.tournament.handler.generator.KnockoutTests.testGenerate(KnockoutTests.java:67)

testGenerate[1: Test with tournamentSize=2, expectedMatchSize: 1, expectedMaxRound: 1](pkg.service.tournament.handler.generator.KnockoutTests)  Time elapsed: 0 sec  <<< ERROR!
java.lang.ArrayIndexOutOfBoundsException: 35
    at pkg.model.entities.RoundDefinition.hashCode(RoundDefinition.java:26)
    at java.util.HashMap.hash(HashMap.java:339)
    at java.util.HashMap.put(HashMap.java:612)
    at java.util.HashSet.add(HashSet.java:220)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
    at java.util.HashSet.<init>(HashSet.java:120)
    at pkg.service.tournament.handler.generator.KnockoutTests.testGenerate(KnockoutTests.java:67)

lombok生成的代码

public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $id = this.getId();
        int result = result * 59 + ($id == null ? 43 : $id.hashCode());
        Object $round = this.getRound();
        result = result * 59 + ($round == null ? 43 : $round.hashCode());
        Object $numberOfRoundresults = this.getNumberOfRoundresults();
        result = result * 59 + ($numberOfRoundresults == null ? 43 : $numberOfRoundresults.hashCode());
        return result;
    }

#2编辑:

切换到显式生成

Set<RoundDefinition> definitions = new HashSet<>(  );
        for (RoundDefinition rd : roundDefinitions) {
            RoundDefinition asd = new RoundDefinition();
            asd.setRound( rd.getRound() );
            asd.setNumberOfRoundresults( rd.getNumberOfRoundresults() );
            definitions.add(asd); // <-- ArrayIndexOutOfBoundsException
        }

#3编辑:

出于好奇,我试图将元素添加到另一组类中

Set<Tournament> tournamentSet = new HashSet<>();
tournamentSet.add(new Tournament()); <-- ArrayIndexOutOfBoundsException

还将此类初始化添加到其他测试中...相同的行为 但是我可以将字符串添加到HashSet<String> ...到底是怎么回事:-D

#4编辑:

检查了依赖项是否发生更改,但是它们似乎与源分支相同。也可以在源分支中重现...

#5编辑:

从我的模型“ RoundDefinition”中删除了lombok注释“ @Data”,自己写了getter和setter,测试再次正常进行... 使用龙目岛v1.18.8

那么当lombok生成hashCode和equals函数时,为什么会失败?...

1 个答案:

答案 0 :(得分:0)

Soooo我终于可以继续前进了……

当我测试了几次尝试时,我注意到我的测试以某种方式再次起作用。

当我使用配置文件运行测试时,它们会失败,而当没有配置文件的运行时,它们会起作用。 经过一些挖掘和重构后,我进入了项目的pom文件...

插件“ maven-sufefire-plugin”被添加了2次

<project>
<profiles>
[...]
<profile>
 <id>unit-test</id>
  <plugins>
   [...]
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
     <argLine>
      ${argLine} -Dfile.encodeing=utf-8 -Duser.timezone=UTC
     </argLine>
    </configuration>
   </plugin>
[...]
<project>
 <build>
  <plugins>
   [...]
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
     <argLine>
      -Dfile.encodeing=utf-8 -Duser.timezone=UTC
     </argLine>
    </configuration>
   </plugin>
[...]

,但pom文件中根本没有设置属性${argLine}。 因此,我引入了一个新属性<argLine>并将这些参数放入其中。 不知道为什么在hashcode方法中会引发异常,但这也许对其他人也有帮助:-)