简体   繁体   English

带有SpringBoot的GraphQLTester无法实例化xxx指定的class是一个接口

[英]GraphQLTester with SpringBoot Failed to instantiate xxx specified class is an interface

PlayerController.java: PlayerController.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){}
}

PlayerRepository.java PlayerRepository.java

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


}

schema.graphqls: schema.graphqls:

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!
}

PlayerControllerIntTest.java PlayerControllerIntTest.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);
    }
}

I am trying to write simple Unit test to get all of the players.我正在尝试编写简单的单元测试来获取所有玩家。 However no matter how much I researched I haven't been able to find a solution I am getting this error:但是,无论我进行了多少研究,我都无法找到解决方案,我收到了这个错误:

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

Things I have tried:我尝试过的事情:

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

I added these annotations at the top of the PlayerControllerIntTest Then I get a different error saying:我在PlayerControllerIntTest的顶部添加了这些注释然后我得到一个不同的错误说:

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

This is how my project structure looks like: structure这是我的项目结构的样子:结构

Any help?有什么帮助吗?

Alright, by digging into spring-graphql sample codes好吧,通过深入spring-graphql示例代码

I have found a solution.我找到了解决方案。

It was as simple as changing this:就像改变这个一样简单:

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

to this:对此:

@SpringBootTest
@AutoConfigureGraphQlTester
public class PlayerControllerIntTest {

Hope it helps!希望能帮助到你!

Reference: https://github.com/spring-projects/spring-graphql/blob/main/samples/webmvc-http/src/test/java/io/spring/sample/graphql/repository/ArtifactRepositoriesTests.java参考: https://github.com/spring-projects/spring-graphql/blob/main/samples/webmvc-http/src/test/java/io/spring/sample/graphql/repository/ArtifactRepositoriesTests.Z93F725A07423FE1C889F448B33D21F46

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法实例化UriInfo指定的类是接口 - Failed to instantiate UriInfo Specified class is an interface Spring Boot WebFluxTest 测试,无法实例化存储库,指定类为接口 - Spring Boot WebFluxTest test, failed to instantiate repository, specified class is an interface 无法实例化[org.springframework.data.domain.Pageable]:指定的类是接口 - Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface spring boot - 如何在HTTP控制器处理程序中避免“无法实例化[java.util.List]:指定的类是一个接口”? - spring boot - how to avoid “Failed to instantiate [java.util.List]: Specified class is an interface” in HTTP controller handler? Spring插入错误无法实例化[java.util.Map]:指定的类是一个接口 - Spring insert error Failed to instantiate [java.util.Map]: Specified class is an interface 无法实例化[java.util.List]:指定的类是GET请求中的接口 - Failed to instantiate [java.util.List]: Specified class is an interface in GET request org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.ui.Model]:指定的类是接口 - org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.ui.Model]: Specified class is an interface Spring Mongodb-无法实例化 bean class [java.util.List]:指定 class 是一个接口 - Spring Mongodb- Could not instantiate bean class [java.util.List]: Specified class is an interface spring MVC3错误无法实例化bean类[java.util.List]:指定的类是偶尔的接口 - spring MVC3 errors Could not instantiate bean class [java.util.List]: Specified class is an interface Occasional DynamoDBMapper - 无法实例化类 - DynamoDBMapper - Failed to instantiate class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM