BAEL-5778 @RunWith(SpringRunner.class) vs @RunWith(MockitoJUnitRunner.class) (#12991)

This commit is contained in:
Mateusz Szablak 2022-11-14 13:33:34 +01:00 committed by GitHub
parent 7a20c23f0e
commit b55616cdb8
8 changed files with 155 additions and 2 deletions

View File

@ -26,6 +26,9 @@
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<version>${mockito.version}</version> <version>${mockito.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -0,0 +1,17 @@
package com.baeldung.runners;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
public class DataProvider {
private final List<String> memory = Stream.of("baeldung", "java", "dummy").collect(Collectors.toList());
public Stream<String> getValues() {
return memory.stream();
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.runners;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class StringConverter {
private final DataProvider dataProvider;
@Autowired
public StringConverter(DataProvider dataProvider) {
this.dataProvider = dataProvider;
}
public List<String> convert() {
return dataProvider.getValues().map(String::toUpperCase).collect(Collectors.toList());
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.runners;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StringConverterStarter {
public static void main(String[] args) {
SpringApplication.run(StringConverterStarter.class, args);
}
}

View File

@ -1 +1,2 @@
spring.main.allow-bean-definition-overriding=true spring.main.allow-bean-definition-overriding=true
spring.main.web-application-type=none

View File

@ -0,0 +1,30 @@
package com.baeldung.runners;
import lombok.val;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.stream.Stream;
@RunWith(MockitoJUnitRunner.class)
public class StringConverterMockitoRunnerUnitTest {
@Mock
private DataProvider dataProvider;
@InjectMocks
private StringConverter stringConverter;
@Test
public void givenStrings_whenConvert_thenReturnUpperCase() {
Mockito.when(dataProvider.getValues()).thenReturn(Stream.of("first", "second"));
val result = stringConverter.convert();
Assertions.assertThat(result).contains("FIRST", "SECOND");
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.runners;
import lombok.val;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.stream.Stream;
@ContextConfiguration(classes = StringConverter.class)
@RunWith(SpringRunner.class)
public class StringConverterSpringRunnerUnitTest {
@MockBean
private DataProvider dataProvider;
@Autowired
private StringConverter stringConverter;
@Test
public void givenStrings_whenConvert_thenReturnUpperCase() {
Mockito.when(dataProvider.getValues()).thenReturn(Stream.of("first", "second"));
val result = stringConverter.convert();
Assertions.assertThat(result).contains("FIRST", "SECOND");
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.runners;
import lombok.val;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import java.util.stream.Stream;
public class StringConverterWithInitUnitTest {
@Mock
private DataProvider dataProvider;
@InjectMocks
private StringConverter stringConverter;
@Before
public void init() {
MockitoAnnotations.openMocks(this);
// or
// dataProvider = Mockito.mock(DataProvider.class);
// stringConverter = new StringConverter(dataProvider);
}
@Test
public void givenStrings_whenConvert_thenReturnUpperCase() {
Mockito.when(dataProvider.getValues()).thenReturn(Stream.of("first", "second"));
val result = stringConverter.convert();
Assertions.assertThat(result).contains("FIRST", "SECOND");
}
}