2017-11-03 6 views
0

私はRESTEasyののWebサービスが含まれている春のブートアプリケーションを持っては次のように@Serviceを使用して作成:春ブーツRESTEasyのPostメソッドが予想:201実際:404

@Path("/developers") 
@Service 
public interface DeveloperResource { 
@POST 
@Produces("application/json") 
@Consumes("application/json") 
Response create(@RequestBody List<DeveloperDto> developers); 
} 

と私は

応じて統合テストクラスを持っています私が得たテストの実行後
@RunWith(SpringRunner.class) 
@SpringBootTest(classes = Application.class) 
public class DeveloperResourceTest { 

    public static final String URI = "http://localhost:8080/developers"; 
    public static final DeveloperDto DEVELOPER = new DeveloperDto(null, "toto"); 
    public static final List<DeveloperDto> DEVELOPERS_COLLECTION = Collections.singletonList(DEVELOPER); 
    public static final DeveloperEntity DEVELOPER_MAPPED_TO_ENTITY = DeveloperMapper.toEntity(DEVELOPER); 
    public static final String DEVELOPER_COLLECTION_IN_JSON = "[{\"developerId\":null,\"developerName\":\"toto\",\"programmingLanguages\":null}]"; 


    private MockMvc mockMvc; 

    @MockBean 
    DeveloperService service; 

    @Mock 
    private DeveloperResource tested; 

    @Autowired 
    WebApplicationContext webApplicationContext; 

    private ObjectMapper mapperJson; 

    @Before 
    public void setUp() throws Exception { 
     tested=new DeveloperResourceImpl(service); 
     mockMvc=MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build(); 
     mapperJson = new ObjectMapper(); 
    } 

    @Test 
    public void should_create_a_developer_and_return_OK() throws Exception { 
     Mockito.when(service.save(DEVELOPER_MAPPED_TO_ENTITY)).thenReturn(Optional.of(DEVELOPER_MAPPED_TO_ENTITY)); 

     tested.create(DEVELOPERS_COLLECTION); 

     RequestBuilder requestBuilder = MockMvcRequestBuilders 
       .post(URI) 
       .content(DEVELOPER_COLLECTION_IN_JSON) 
       .contentType(MediaType.APPLICATION_JSON); 

     MvcResult result = mockMvc.perform(requestBuilder).andReturn(); 

     MockHttpServletResponse response = result.getResponse(); 

     assertEquals(HttpStatus.CREATED.value(), response.getStatus()); 
     assertEquals(URI, response.getHeader(HttpHeaders.LOCATION)); 

    } 
} 

java.lang.AssertionError: Expected :201 Actual :404

は私の質問は以下のとおりです。

  1. 私の統合テストの設定は適切ですか?
  2. 作成したREST Webサービスが@RestControllerまたは@Controller?を使用して作成されていない場合でもMockMvcを使用できますか?

+0

application.propertiesを投稿できますか?それは使用して春のブート自動設定だから返事を、私はプロパティが設定用のファイルがありません – DeadSpock

+0

感謝の: 「@SpringBootApplication」 「@ComponentScan」 私はapplication.yaml持っているが、それだけで持続性が含まれています構成: サーバーポート:8080 jpa hibernate auto データベースURL – essalprod

答えて

0

@Serviceは、RESTサービスとして認識されることはありません事前にありがとうございます。クラスの@RestControllerとして注釈を付ける必要があります。実際にはcreateメソッドを実装しています。 参照:@Service

0

MockMvcを使用して、Spring MVCを使用しないアプリケーションをテストすることはできません。 RESTdocsは、HTTP経由で動作するRest Assuredをサポートしているため、Webスタックに依存しません。