Have a class in the package com.conf
#Configuration
public class WebConfTest {
#Autowired
private Environment environment;
}
and unit test into com.service
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = { WebConfTest.class })
public class DMSServiceImplTest {
#Autowired
WebConfTest webConfTest;
#Test
public void testConnect() throws Exception {
}
}
test dependency :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
In the IDEA navigation between beans work. But WebConfTest== null if I run test.
What is wrong?
Thanks.
#RunWith is for junit runner.
If you want to run tests with TestNG, you need to extends AbstractTestNGSpringContextTests.
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-support-classes-testng
Related
I have created a springboot project splitted into three maven modules, the domain layer, the core layer (contains persistence and business logic) and the web layer. I try to unit test my repository ProductRepository (located in the core layer)
#RunWith(SpringRunner.class) // provide bridge between SpringBoot test features and JUnit, for usage of springboot tsting features
#DataJpaTest
class ProductRepositoryTest {
#Autowired
private TestEntityManager em;
#Autowired
private ProductRepository repository;
#Test
void shouldReturnProduct() {
// given
Product p = Product.builder().id(1).designation("Test").reference("TEST").unitPrice(150).build();
this.em.persistAndFlush(p);
// when
Product found = repository.findByReference(p.getReference());
// then
assertThat(found.getReference()).isEqualTo(p.getReference());
}
}
But the repository is always instanciated to null. I run this test as JUnit Test in eclipse and i got a nullpointerexception.
Here is my pom.xml file
<dependencies>
<!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
You say you try to unit test the controller but you use #RunWith(SpringRunner.class), this is used for integration tests. This annotation starts the complete application. And you just want to test the repository. What you can do is create an abstract DAO which you can implement in your unit tests.
public abstract class AbstractRepository<RepositoryType> {
RepositoryType repository;
private DBI dbi;
protected abstract RepositoryType createRepository(final DBI dbi);
#Before
public final void setUpDataSource() throws Exception {
final JdbcDataSource jdbcDataSource = new JdbcDataSource();
// DB_CLOSE_DELAY=-1 ==> h2 will keep its content as long as the vm lives otherwise the content of the database
// is lost at the moment the last connection is closed.
jdbcDataSource
.setURL("jdbc:h2:mem:play;MODE=MySQL;DB_CLOSE_DELAY=-1L;INIT=RUNSCRIPT FROM 'classpath:path/to/file/init_test.sql';");
final Flyway flyway = new Flyway();
flyway.setDataSource(jdbcDataSource);
flyway.setLocations("/path/to/locations");
flyway.migrate();
dbi = new DBI(jdbcDataSource);
runDbScript("/data.sql");
repository = createRepository(dbi);
}
private void runDbScript(final String scriptPath) throws Exception {
try (InputStreamReader reader = new InputStreamReader(AbstractDaoTest.class.getResourceAsStream(scriptPath),
Charsets.UTF_8); Handle h = dbi.open()) {
RunScript.execute(h.getConnection(), reader);
}
}
}
Now you can overwrite the createRepository method in your test class.
public class ProductRepositoryTest() {
#Override
protected ProductRepository createRepository(Dbi dbi) { return new ProductRepository(dbi); }
#Test
public void testGetProductById() {
Product response = repository.getProductById(1);
assertThat(response).isEqualTo(someObject);
}
}
If you need a framework to mock objects you can use Mockito and if you need to mock static or void methods you can use PowerMock.
Hope this helps.
Add these annotations to your test classes.
#SpringBootTest
#TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DbUnitTestExecutionListener.class,
TransactionalTestExecutionListener.class })
public class YourTestClass {....
here's a working version of your example - hope this helps. I think you may have some conflicting configuration or dependencies in your own project. https://github.com/tndavidson/springbootjparepositorytest
I am using Spring Configuration and Jersey Configuration.
This is my spring configuration:
#Component
#Configuration
#EnableScheduling
#EnableAspectJAutoProxy
#EnableTransactionManagement
#ComponentScan(basePackages = { "my.packages" })
public class SpringConfig {
private static final String MESSAGE_SOURCE_BASE_NAME = "i18n/messages";
#Profile(Profiles.APPLICATION)
#Configuration
#PropertySource("classpath:application.properties")
static class ApplicationProperties {
}
#Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder(11);
}
#Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler();
}
#Bean
MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename(MESSAGE_SOURCE_BASE_NAME);
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
}
This is my Service class and interface:
public interface DAOService {
User register(User user)
}
#Service
public class DAOServiceImpl implements DAOService {
#Override
User register(User user){
return null;
}
}
This is my jersey resource:
Inside ResourceSecurityService class I want to use #Autowire for DAOService
class, but I get null pointer exception, daoService is always null.
#Component
#Path("/security")
#Produces({ MediaType.APPLICATION_JSON })
public class ResourceDAOService {
#Autowired
private DAOService daoService ; //<--- here daoService is null;
#POST
#Path("/register")
#Consumes({ MediaType.APPLICATION_JSON })
#Transactional
public Response register(User user, #Context HttpServletRequest request,
#Context HttpServletResponse response) throws AppException {
return Response.status(Response.Status.CREATED).entity(securityService.register(user)).build();
}
I am using spring version 4.3.8.RELEASE with the next dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
It could be better if you'd provide a link to your project.
Based on information I see:
make sure you have jersey-spring4 dependency
make sure your Jersey version compatible with integration dependency
check out your project configuration
I advice you to use Spring Boot with auto configurations.
This code compiles and debugs well, but when I do a maven build in Eclipse the unit test and the build fails. I don't understand where is the misuse of the matchers here? Thanks.
[ERROR] Errors: [ERROR] Tests.MyTest() ยป InvalidUseOfMatchers
#RunWith(SpringRunner.class)
#ContextConfiguration(classes = {MapperFactory.class})
public class Tests {
#Mock private Bucket bucketMock;
#Mock private MutateInBuilder builderMock;
#InjectMocks private Repository couchbaseRepository;
private MapperFactory mapperFactory;
#Autowired
public void setMapperFactory(MapperFactory mapperFactory) {
this.mapperFactory = mapperFactory;
}
#Test
public void MyTest() throws MyException {
String jsonText = jsonSamples.getProperty("theJson");
Mapper mapper = mapperFactory.getMapper(JsonObject.fromJson(jsonText),repository);
when(bucketMock.mutateIn("1234")).thenReturn(builderMock);
mapper.execute();
verify(builderMock).execute();
}
}
Thanks for your help, that right, that was all the exception was saying. The solution was to update the artifactId of Mockito in the pom.xml file:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
was replaced with:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
</dependency>
In my spring boot application, I have the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
I have a method in the below class which i need to test:
#Service
public class DataExtractorService {
#Autowired
LinksWriterService writer;
Kinkester kinkester;
public DataExtractorService(){
kinkester=new Kinkester();
}
public Kinkester extractor(String rowData){
String pattern = "(\\d+)(\\D*)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(rowData);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
}else
System.out.println("NO MATCH");
kinkester.setAge(Integer.parseInt(m.group(0)));
kinkester.setRole(m.group(1));
return kinkester;
}
}
and then the test class is:
#RunWith(SpringRunner.class)
#SpringBootTest(classes = SeleniumApplication.class)
#WebAppConfiguration
public class DataExtractorTest {
#Autowired
DataExtractorService dataExtractorService;
#Test
public void extractor(){
Kinkester kinkester = dataExtractorService.extractor("45M");
System.out.println(kinkester.getAge());
//assertEquals(kinkester.getAge(),45);
}
}
But unfortunately the test does not run. it complains with
Initialization Error (Runner: Junit 4 )
I have tried the below code, but still no answer got:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {SeleniumApplication.class})
Your test class should be annotated with:
#RunWith(SpringJUnit4ClassRunner.class)
#SpringBootTest(classes = SeleniumApplication.class)
This JUnit runner requires JUnit version >= 4.12. Your dependency on spring-boot-starter-test will bring the required version of JUnit in transitively so you can remove the dependency on JUnit. Your explcit dependency on JUnit 4.11 is clashing with the Spring runner's JUnit expectations.
Hy guys,
I am trying to make a test using mockito on a web application that uses spring mvc.
When it executes this line "Mockito.reset(notificacaoRepositoryMock);" it throws "org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class com.sun.proxy.$Proxy40"
I saw that in am example and it worked, I can't find what I am doing wrong here.
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {TestContext.class, WebAppConfig.class})
#WebAppConfiguration
public class NotificacaoControllerTest {
private MockMvc mockMvc;
#Autowired
private NotificacaoRepository notificacaoRepositoryMock;
#Autowired
private WebApplicationContext webApplicationContext;
#Before
public void setUp() {
// *** Error here ***
Mockito.reset(notificacaoRepositoryMock);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
My TestContext is:
#Configuration
public class TestContext {
#Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("i18n/messages");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
#Bean
public NotificacaoRepository notificacaoRepository() {
return Mockito.mock(NotificacaoRepository.class);
}
}
The class I want mock is a CrudRepository interface
public interface NotificacaoRepository extends CrudRepository<Notificacao, Long> {
}
and, I think, the relevant part of my pom.xml is (spring versions and mockito)
<properties>
<hibernate.version>4.2.0.Final</hibernate.version>
<mysql.connector.version>5.1.21</mysql.connector.version>
<spring.version>4.1.6.RELEASE</spring.version>
<spring.data.version>1.8.0.RELEASE</spring.data.version>
</properties>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
UPDATE
#jfcorugedo i tryed exactly what you said but I keep receiving the same error. My test context is just
#Configuration
public class TestContext {
#Bean
#Primary
public NotificacaoRepository notificacaoRepository() {
return Mockito.mock(NotificacaoRepository.class);
}
}
and my test class now is:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {WebAppConfig.class})
#WebAppConfiguration
#Import({TestContext.class})
public class NotificacaoControllerTest {
#Autowired
NotificacaoRepository notificacaoRepositoryMock;
#Before
public void setUp() {
Mockito.reset(notificacaoRepositoryMock); // Error >> org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class com.sun.proxy.$Proxy55
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
This is because Spring create a proxy around your bean.
Try to not inject mock using Spring
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {TestContext.class, WebAppConfig.class})
#WebAppConfiguration
public class NotificacaoControllerTest {
private MockMvc mockMvc;
private NotificacaoRepository notificacaoRepositoryMock;
#Autowired
private WebApplicationContext webApplicationContext;
#Before
public void setUp() {
notificacaoRepositoryMock = Mockito.mock(NotificacaoRepository.class);
Mockito.reset(notificacaoRepositoryMock);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
As far as I know, you are trying to replace the spring bean with a mock version.
In that case you have to annotate the method that produces your mock with the annotation #Primary, so Spring could choose the mock object by default in each autowired field (of course if it doesn't have any qualifier).
If you're trying to use a Spring context in your test that has some real beans and other mock beans, you have to follow this steps:
Create a #Configuration class in your test folder that inject a mock instance of the beans you want to mock
Import this configuration class in your test
For instance:
Configuration class that injects the mock
import static org.mockito.Mockito.mock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import xxx.xxx.xxx.NotificacaoRepository;
#Configuration
public class MockConfigurer {
#Bean
#Primary
public NotificacaoRepository registerMock() {
return mock(NotificacaoRepository.class);
}
}
Test class
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = {WebAppConfig.class})
#WebAppConfiguration
#Import({MockConfigurer.class})
public class NotificacaoControllerTest {
//Now the instance injected here should be a mock object
#Autowired
private NotificacaoRepository notificacaoRepositoryMock;
...
}