It appears to be a syntax error though I can't seem to find it in my code.
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import com.hoaxify.user.User;
import com.hoaxify.user.UserRepository;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class UserControllerTest {
private static final String API_1_0_USERS = "/api/1.0/users";
@Autowired
TestRestTemplate testRestTemplate;
@Autowired
UserRepository userRepository;
@Before
public void cleanUp() {
userRepository.deleteAll();
}
@Test
public void postUser_whenUserIsValid_recievOk() {
User user = createValidUser();
ResponseEntity<Object> response = testRestTemplate.postForEntity(API_1_0_USERS, user, Object.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
private User createValidUser() {
User user = new User();
user.setUsername("test-user");
user.setDisplayName("test-display");
user.setPassword("P4ssword");
return user;
}
@Test
public void postUser_whenUserIsValid_userSavedToDatabase() {
User user = createValidUser();
testRestTemplate.postForEntity(API_1_0_USERS, user, Object.class);
assertThat(userRepository.count()).isEqualTo(1);
}
Here is a failure trace
org.springframework.dao.InvalidDataAccessResourceUsageException:
could not prepare statement [Syntax error in SQL statement
"select u1_0.id,u1_0.display_name,u1_0.password,u1_0.username
from [*]user u1_0"; expected "identifier"; SQL statement:
select u1_0.id,u1_0.display_name,u1_0.password,u1_0.username from user u1_0 [42001-232]] [select
u1_0.id,u1_0.display_name,u1_0.password,u1_0.username from user u1_0]; SQL [select u1_0.id,u1_0.display_name,u1_0.password,u1_0.username from user u1_0]
Not sure what happened but it's asuming that I'm requesting a different type of format for creating the user but I can't find the option their referring to. I'm so confused.
"select u1_0.id,u1_0.display_name,u1_0.password,u1_0.username from [*]user u1_0"; expected "identifier";
or highlighted differently:
select u1_0.id,u1_0.display_name,u1_0.password,u1_0.username
from user u1_0"; expected "identifier"
^^^^
user
is a reserved keyword in most databases. If you want to use it as a table name, you have to somehow quote or escape the keyword so that it can be used as an identifier. Easiest option is to rename your table (or your entity).
You probably have somewhere:
@Entity
public class User {
// …
}
Change that to
@Entity
@Table(name = "user_info")
public class User { … }
Which will create your table with name user_info
, which is a valid identifier and not a reserved keyword.