Reputation: 131
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
@Repository
public interface PlayerRepository extends CrudRepository<Player,Long> {
}
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
@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:
java.lang.IllegalArgumentException: Unrecognized Type: org.springframework.core.ResolvableType$EmptyType@5333f08f
This is how my project structure looks like: structure
Any help?
Upvotes: 8
Views: 1529
Reputation: 131
Alright, by digging into spring-graphql
sample codes
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!
Upvotes: 5