junit 使用SpringBoot的GraphQLTester无法示例化xxx指定的类是接口

pbossiut  于 2022-11-11  发布在  Spring
关注(0)|答案(1)|浏览(173)

播放器控制器.java:

@Controller
public class PlayerController {

    private final int MAXIMUM_CAPACITY = 12;
    @Autowired
    private final PlayerRepository playerRepository;

    public PlayerController(PlayerRepository repository){
        this.playerRepository = repository;

    }

    @QueryMapping
    List<Player> getAllPlayers(){
        List<Player> ps = new ArrayList<>();
        playerRepository.findAll().forEach(ps::add);
        return ps;
    }

    @QueryMapping
    Optional<Player> playerById(@Argument Long id){
        return playerRepository.findById(id);

    }

    @MutationMapping
    Object AddPlayer(@Argument PlayerInput player){

        if(playerRepository.count() >= MAXIMUM_CAPACITY)
            return new PlayerFailedPayload("maximum number of players reached (" + MAXIMUM_CAPACITY + ")! Please delete players before adding more." );

        if(!PlayerPosition.isValidPosition(player.position()))
            return new PlayerFailedPayload("Invalid Player Position, The valid positions are: {'PG','SG','SF','PF','C'}");

        if(player.name().isEmpty() || player.surname().isEmpty())
            return new PlayerFailedPayload("Name or surname cannot be empty");

        Player p = new Player(player.name(),player.surname(),player.position());
        return new PlayerSuccessPayload("A new player was added successfully." , playerRepository.save(p));
    }

    @MutationMapping
    Object DeletePlayer(@Argument Long id) {
        Optional<Player> player = playerRepository.findById(id);

        if(player.isEmpty())
            return new PlayerFailedPayload("player with id " + id + " does not exist!");
        playerRepository.deleteById(id);
        return new PlayerSuccessPayload("Player with id " + id + " was deleted successfully",player.get());

    }

    record PlayerInput(String name, String surname, String position){}
    record PlayerSuccessPayload(String message,Player player){}
    record PlayerFailedPayload(String error){}
}

播放器资源库.java

@Repository
public interface PlayerRepository extends CrudRepository<Player,Long> {

}

架构.图形:

union PlayerPayload = PlayerSuccessPayload | PlayerFailedPayload

type PlayerFailedPayload {
    error: String!
}

type PlayerSuccessPayload {
    message: String!
    player : Player!
}

type Query {
    getAllPlayers: [Player]
    playerById(id: ID!): Player!
}

type Mutation{
    AddPlayer(player: PlayerInput): PlayerPayload!
    DeletePlayer(id : ID!): PlayerPayload!
}

input PlayerInput {
    name: String!
    surname: String!
    position: String!
}

type Player {
    id: ID!
    name: String!
    surname: String!
    position: String!
}

播放器控制器集成测试.java

@GraphQlTest(PlayerController.class)
@Import(PlayerRepository.class)
class PlayerControllerIntTest {

    @Autowired
    GraphQlTester graphQlTester;

    @Test
    void testGetAllPlayersShouldReturnAllPlayers() {
        // language=GraphQL
        String document = """
        query {
            getAllPlayers {
                id
                name
                surname
                position         
            }
        }    
        """;

        graphQlTester.document(document)
                .execute()
                .path("getAllPlayers")
                .entityList(Player.class)
                .hasSize(3);
    }
}

我试图写一个简单的单元测试来得到所有的球员。然而,无论我怎么研究,我还没有能够找到一个解决方案,我得到这个错误:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'playerController' defined in file [E:\Documents\Github\Java\basketball\basketball\target\classes\com\example\basketball\controller\PlayerController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example.basketball.repository.PlayerRepository': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.basketball.repository.PlayerRepository]: Specified class is an interface

我尝试过的方法:

@EnableAutoConfiguration
@ContextConfiguration(classes = {PlayerRepository.class})

我在PlayerControllerIntTest的顶部添加了这些注解,然后我得到一个不同的错误消息:

java.lang.IllegalArgumentException: Unrecognized Type: org.springframework.core.ResolvableType$EmptyType@5333f08f

我的项目结构如下所示:structure
有什么帮助吗?

iklwldmw

iklwldmw1#

好的,通过深入研究spring-graphql示例代码
我找到了解决办法。
就这么简单:

@GraphQlTest(PlayerController.class)
@Import(PlayerRepository.class)
class PlayerControllerIntTest {

更改为:

@SpringBootTest
@AutoConfigureGraphQlTester
public class PlayerControllerIntTest {

希望能有所帮助!
参考:https://github.com/spring-projects/spring-graphql/blob/main/samples/webmvc-http/src/test/java/io/spring/sample/graphql/repository/ArtifactRepositoriesTests.java

相关问题