Commit 23ea0734 authored by vikram singh's avatar vikram singh

segregated integration and component test cases

parent 942fa67b
...@@ -2,12 +2,14 @@ package com.nisum.webflux.controller; ...@@ -2,12 +2,14 @@ package com.nisum.webflux.controller;
import com.nisum.webflux.model.Employee; import com.nisum.webflux.model.Employee;
import com.nisum.webflux.service.IEmployeeService; import com.nisum.webflux.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@RestController @RestController
@Slf4j
public class EmployeeController { public class EmployeeController {
@Autowired @Autowired
...@@ -17,7 +19,7 @@ public class EmployeeController { ...@@ -17,7 +19,7 @@ public class EmployeeController {
@PostMapping("/employee") @PostMapping("/employee")
public Mono<Employee> save(@RequestBody public Mono<Employee> save(@RequestBody
Employee employee) { Employee employee) {
System.out.println("employee obj ##################################################"+employee); log.info("employee obj ##################################################"+employee);
return employeeService.save(employee); return employeeService.save(employee);
} }
......
...@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@Slf4j @Slf4j
//basic kafka
public class SampleController { public class SampleController {
@Autowired @Autowired
KafkaServer kafkaServer; KafkaServer kafkaServer;
......
...@@ -30,17 +30,12 @@ import static org.mockito.Mockito.when; ...@@ -30,17 +30,12 @@ import static org.mockito.Mockito.when;
@SpringBootTest @SpringBootTest
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@AutoConfigureWebTestClient @AutoConfigureWebTestClient
public class EmployeeControllerTest { public class EmployeeControllerIntegrationTest {
@Autowired @Autowired
WebTestClient webTestClient; WebTestClient webTestClient;
//// @MockBean
// @Autowired
// EmployeeRepo empRepo;
@InjectMocks @InjectMocks
EmployeeController employeeController; EmployeeController employeeController;
......
package com.nisum.webflux.controller; package com.nisum.webflux.controller;
import com.nisum.webflux.model.Fruit; import com.nisum.webflux.model.Fruit;
import com.nisum.webflux.repository.FruitRepository;
import com.nisum.webflux.service.impl.TestConfig;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
import reactor.core.publisher.Mono;
import static org.mockito.Mockito.when;
@SpringBootTest @SpringBootTest
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@AutoConfigureWebTestClient @AutoConfigureWebTestClient
//@ContextConfiguration(classes = TestConfig.class) @ContextConfiguration(classes = TestConfig.class)
public class FruitControllerComponentTest { public class FruitControllerComponentTest {
@Autowired
@Autowired
WebTestClient webTestClient; WebTestClient webTestClient;
@Test @Autowired
public void save_UsingWebTestClient() { FruitRepository fruitRepo;
Please register or sign in to reply
Fruit fruit=new Fruit("1","apple","300"); @Test
webTestClient.post().uri("/fruit").contentType(MediaType.APPLICATION_JSON_UTF8).body(BodyInserters.fromObject(fruit)) public void save() {
.exchange() ArgumentCaptor<Fruit> requestCaptor = ArgumentCaptor.forClass(
.expectStatus().isOk().expectBody().jsonPath("$.id").isEqualTo("1"); Fruit.class);
} when(fruitRepo.save(requestCaptor.capture())).thenReturn(Mono.error(new RuntimeException()));
Fruit fruit=new Fruit("1","apple","300");
webTestClient.post().uri("/fruit").contentType(MediaType.APPLICATION_JSON_UTF8).body(BodyInserters.fromObject(fruit))
.exchange()
.expectStatus().isOk().expectBody().jsonPath("$.id").isEqualTo("1");
}
} }
package com.nisum.webflux.controller;
import com.nisum.webflux.model.Fruit;
import com.nisum.webflux.repository.FruitRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.BodyInserters;
import reactor.core.publisher.Mono;
import static org.mockito.Mockito.when;
@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureWebTestClient
public class FruitControllerIntegrationTest {
@Autowired
WebTestClient webTestClient;
Please register or sign in to reply
@Test
public void save() {
Fruit fruit= new Fruit("1","apple","300");
webTestClient.post().uri("/fruit").contentType(MediaType.APPLICATION_JSON_UTF8).body(BodyInserters.fromObject(fruit))
.exchange()
.expectStatus().isOk().expectBody().jsonPath("$.id").isEqualTo("1");
}
}
package com.nisum.webflux.service.impl; package com.nisum.webflux.service.impl;
import com.nisum.webflux.repository.EmployeeRepo; import com.nisum.webflux.repository.EmployeeRepo;
import com.nisum.webflux.repository.FruitRepository;
import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
...@@ -9,5 +10,7 @@ public class TestConfig { ...@@ -9,5 +10,7 @@ public class TestConfig {
@MockBean @MockBean
EmployeeRepo empRepo; EmployeeRepo empRepo;
@MockBean
FruitRepository fruitRepo;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment