実現したいこと
SpringBootで書かれたControllerのpostメソッドを単体テストしたいです。
ただただ、v1DemoPostをテスト実行できればよいのですが、JUnitの経験がなく既存プロジェクトの追加が必要ですが、設定含めて詰まってしまっています。
前提
InteliJのライブラリからjunit\4.9\junit-4.9.jarを追加
発生している問題・エラーメッセージ
コピペできないので申し訳ないですが、以下エラーが出ます。invalidTestClassError@BootstrapWithの宣言が複数見つかりました
@RunWithを削除すると、エラーが変わりSLF4J: クラスパスには複数のSLF4Jプロバイダーが含まれています。
となりますが、InitializationErrorがなくなります。
該当のソースコード
Controller
1package sample.controller; 2 3import sample.config.ApiClientConfig; 4import sample.controller.DemoApi; 5import sample.controller.model.DemoReq; 6import sample.controller.model.DemoRes; 7import org.model.ModelMapper; 8import org.springframework.beans.factory.annotation.Autowired; 9import org.springframework.http.*; 10import org.springframework.web.bind.annnotation.PostMapping; 11import org.springframework.web.bind.annnotation.RestController; 12import org.springframework.web.context.request.NativeWebRequest; 13 14@RestController 15public class DemoController implements DemoApi { 16 @Autowired 17 sample.apiclient.DemoApi apiClient; 18 19 @Autowired 20 ModelMapper modelMapper; 21 22 @Autowired 23 ApiClientConfig apiClientConfig; 24 25 @Override 26 public Optional<NativeWebRequest> getRequest() { 27 return DemoApi.super.getRequest(); 28 } 29 30 @Override 31 @PostMapping("/v1/demo") 32 public ResponseEntity<DemoRes> v1DemoPost(String host, String content, String name, DemoReq demoReq) { 33 sample.apiclient.model.DemoReq req = 34 modelMapper.map(demoReq, sample.apiclient.model.DemoReq.class); 35 36 sample.apiclient.model.DemoRes res = 37 apiClient.v1DemoPost(apiClientConfig.getHost(), content, name, req); 38 39 DemoRes response = modelMapper.map(res, DemoRes.class); 40 return new ResponseEntity<DemoRes>(response, HttpStatus.OK); 41 } 42}
DemoControllerTest
1import sample.controller.DemoController; 2import org.junit.Before; 3import org.junit.Test; 4import org.junit.Runner.RunWith; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; 7import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; 8import org.springframework.boot.test.context.SpringBootTest; 9import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 10import org.springframework.test.web.servlet.MockMvc; 11import org.springframework.test.web.servlet.setup.MockMvcBuilders; 12 13import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; 14import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 15 16@RunWith(SpringJUnit4ClassRunner.class) 17@SpringBootTest 18@WebMvcTest 19@AutoConfigureMockMvc 20public class DemoControllerTest { 21 private MockMvc mockMvc; 22 23 @Autowired 24 DemoController target; 25 26 @Before 27 public void setup() { 28 mockMvc = MockMvcBuilders.standaloneSetup(target).build(); 29 } 30 31 @Test 32 public void POSTテスト() throws Exception { 33 mockMvc.perform(post("/v1/demo")) 34 .andExcect(status().isOk()); 35 } 36}
試したこと
DemoControllerTestに
@EnableAutoConfiguration
@ComponentScan
を追加
または@RunWithを削除
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
IDEA:InteliJ
JUnit:4.9(バージョンは問いません)

0 コメント