writing Test cases for restful service using junit
Date : March 29 2020, 07:55 AM
it helps some times Ok. So there are several things that could / should be done. Unit test the Controller directly. Mock all other classes. Attempt to use any framework provided testing utilities to test how the controller interacts with the framework. An example of this is Spring's MockMVC. For an example, check out the "correct" answer to this question: JUnit test for ExceptionHandler
|
Why is my Spring Boot autowired JPA Repository failing JUnit test?
Date : March 29 2020, 07:55 AM
will help you My JUnit test is failing with the following error: "java.lang.IllegalArgumentException: Could not find field [userRepository] of type [null] on target [com.clearsoft.demo.UserResource@756b2d90]" , Exception comes from this line: ReflectionTestUtils.setField(userResource, "userRepository", userRepository);
|
Mock autowired dependency in JUnit 5 test for Spring Boot 2 app
Date : March 29 2020, 07:55 AM
help you fix your problem Mockito 1 runner (MockitoJUnitRunner class) is not designed to run JUnit 5 tests. So annotating your class with : import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class MyJUnit5Test {
MockitoAnnotations.initMocks(this);
@Component
public class SomeDependency {
public String returnThat() {
return "that";
}
}
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import davidhxxx.example.angularsboot.SomeDependency;
@ExtendWith(MockitoExtension.class)
public class SomeClassTest {
@Mock
SomeDependency someDependencyMock;
private SomeClass someClass;
@BeforeEach
public void setup(){
someClass = new SomeClass(someDependencyMock);
}
@Test
void myFirstTest() {
Mockito.when(someDependencyMock.returnThat()).thenReturn("mock result");
Assertions.assertEquals("mock result", someClass.inc());
}
}
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
...
</dependencies>
<dependencies>
...
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.18.0</version> <!-- version to specify as not provided by Spring Boot dependencies -->
<scope>test</scope>
</dependency>
</dependencies>
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>
...
<plugins>
|
How to write Junit test cases for spring boot application?
Date : March 29 2020, 07:55 AM
it helps some times I have managed to make your code to work. I will post only the changed classes: The interface: public interface DisplayRepository extends CrudRepository<Display, Long> {
Optional<Display> findByTitle(String name);
}
@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class DisplayRepositoryTest {
@Autowired
private TestEntityManager testEntityManager;
@Autowired
private DisplayRepository productRespository;
@Before()
public void setUp(){
Display m = new Display();
// m.setId(2L); // The ID is autogenerated; can retrieve it from the persistAndFlush result
m.setCategory("Group1");
m.setTitle("Product2");
testEntityManager.persistAndFlush(m);
}
@Test
public void whenFindByName_thenReturnProduct() {
// when
Display product = productRespository.findByTitle("Product2").orElseThrow(() -> new RuntimeException("Product not found"));
// then
assertThat(product.getTitle()).isEqualTo("Product2");
}
@Test
public void whenFindAll_thenReturnProductList() {
// when
List<Display> products = (List<Display>) productRespository.findAll();
// then
assertThat(products).hasSize(1);
}
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
|
Can't Autowire Service in JUnit 4 Test in Spring Boot Multi Module Application
Tag : java , By : Nate Bedortha
Date : March 29 2020, 07:55 AM
may help you . You should avoid using field injection (even though is possible) and use constructor injection. This will solve this problem as you will be able to pass the service from the constructor but it will also be useful in the future as you can locate usages and trace objects in your code way better than field injections which are "hidden" So I recommend instead of trying to solve your problem here to refactor your class in constructor injection and pass the service from there either by directly creating the object in your test or by creating a configuration for your test that will generate the object and give the arguments it requires @ContextConfiguration(classes = { GeneralTester.TestConfig.class })
@RunWith(SpringRunner.class)
public class GeneralTester {
@TestConfiguration
public static class TestConfig {
@Bean
public IProductService productService(final IProductRepository productRepository){
return new ProductService(productRepository);
}
@Bean
public IProductRepository productRepository(){
return mock(IProductRepository.class);
}
}
@Autowire
public IProductService productService;
@Autowire
public IProductRepository productRepository;
@Before
public void setUp() {
reset(productRepository);
}
@After
public void tearDown() {
verifyNoMoreInteractions(productRepository);
}
@Test
public void doSmth() {
//... your setup
when(productRepository.save(any())).thenReturn("something");
//... your call and assertions
verify(productRepository).save(any());
}
}
|