Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot how to exclude certain class from testing

I have implemented own class for representing of database vector data based on UserType class.

My example:

class MyVectorType implements UserType {
@Override
    public int[] sqlTypes() {
        return new int[] { Types.ARRAY };
    }
};

@Entity
@Table("MY_ENTITY")
public class MyEntity {
    private MyVectorType myVectorType;

}

However this class cannot be used in testing with h2 dialect ie. in memory database. There is error: No Dialect mapping for JDBC type: 2003.

Therefore I would like to exclude this entity (inc. repository) from testing but this does not work:

@SpringBootApplication
@ComponentScan(excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {
                MyEntity.class, MyEntityRepository.class})
})
public class ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}

What is wrong or is there any best practice solving this problem?

EDIT 1: fixed examples - added correct entity and repository

SOLUTION 1: I think that the only possible solution for this moment is move entity classes (which needs to be excluded) to other package. Then set @EntityScan to scan just non-excluded package. Exclude filters in ComponentScan seems to work only in case of @Component classes, not @Entity. However this is not absolutely best practice to solve this problem.

like image 717
Luke Avatar asked Jan 28 '23 03:01

Luke


1 Answers

Just define it as a @MockBean so the real implementation of your repository will be replaced by a functionless mock in your tests:

@MockBean
private MyVectorRepositoryType vectorRepository;
like image 156
Plog Avatar answered Jan 30 '23 16:01

Plog