chalie001
asked on
size error
hi am geting this error
this my code
this my code
@Test
void processFindFormReturnMany() throws Exception {
when(ownerService.findAllByLastNameLike(anyString()))
.thenReturn(Arrays.asList(Owner.builder().id(1l).build(),
Owner.builder().id(2l).build()));
mockMvc.perform(get("/owners"))
.andExpect(status().isOk())
.andExpect(view().name("owners/ownersList"))
.andExpect(model().attribute("selections", hasSize(2)));
}
The hasSize is in the Matchers class, you probably import only the hamcrest core library. If what mccarl suggests does not work then try to also use the whole hamcrest library (which includes both core and matcher). If you use maven you can add this in your pom
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.1</version>
<scope>test</scope>
</dependency>
ASKER
this the class
this my pom
package guru.springframework.tdmpetclinic.Controllers;
import guru.springframework.tdmpetclinic.module.Owner;
import guru.springframework.tdmpetclinic.services.OwnerService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.*;
import static net.bytebuddy.matcher.ElementMatchers.is;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@ExtendWith(MockitoExtension.class)
class OwnerControllerTest {
@Mock
OwnerService ownerService;
@InjectMocks
OwnerController controller;
Set<Owner> owners;
MockMvc mockMvc;
@BeforeEach
void setUp() {
owners = new HashSet<>();
owners.add(Owner.builder().id(1l).build());
owners.add(Owner.builder().id(2l).build());
mockMvc = MockMvcBuilders
.standaloneSetup(controller)
.build();
}
@Test
void findOwners() throws Exception {
mockMvc.perform(get("/owners/find"))
.andExpect(status().isOk())
.andExpect(view().name("owners/findOwners"))
.andExpect(model().attributeExists("owner"));
verifyZeroInteractions(ownerService);
}
@Test
void processFindFormReturnMany() throws Exception {
when(ownerService.findAllByLastNameLike(anyString()))
.thenReturn(Arrays.asList(Owner.builder().id(1l).build(),
Owner.builder().id(2l).build()));
mockMvc.perform(get("/owners"))
.andExpect(status().isOk())
.andExpect(view().name("owners/ownersList"))
.andExpect(model().attribute("selections", hasSize(2)));
}
@Test
void processFindFormReturnOne() throws Exception {
when(ownerService.findAllByLastNameLike(anyString())).thenReturn(Arrays.asList(Owner.builder().id(1l).build()));
mockMvc.perform(get("/owners"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/1"));
}
@Test
void processFindFormEmptyReturnMany() throws Exception {
when(ownerService.findAllByLastNameLike(anyString()))
.thenReturn(Arrays.asList(Owner.builder().id(1l).build(),
Owner.builder().id(2l).build()));
mockMvc.perform(get("/owners")
.param("lastName",""))
.andExpect(status().isOk())
.andExpect(view().name("owners/ownersList"))
.andExpect(model().attribute("selections", hasSize(2)));;
}
@Test
void displayOwner() throws Exception {
when(ownerService.findById(anyLong())).thenReturn(Owner.builder().id(1l).build());
mockMvc.perform(get("/owners/123"))
.andExpect(status().isOk())
.andExpect(view().name("owners/ownerDetails"))
.andExpect(model().attribute("owner", hasProperty("id", is(1l))));
}
@Test
void initCreationForm() throws Exception {
mockMvc.perform(get("/owners/new"))
.andExpect(status().isOk())
.andExpect(view().name("owners/createOrUpdateOwnerForm"))
.andExpect(model().attributeExists("owner"));
verifyZeroInteractions(ownerService);
}
@Test
void processCreationForm() throws Exception {
when(ownerService.save(ArgumentMatchers.any())).thenReturn(Owner.builder().id(1l).build());
mockMvc.perform(post("/owners/new"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/1"))
.andExpect(model().attributeExists("owner"));
verify(ownerService).save(ArgumentMatchers.any());
}
@Test
void initUpdateOwnerForm() throws Exception {
when(ownerService.findById(anyLong())).thenReturn(Owner.builder().id(1l).build());
mockMvc.perform(get("/owners/1/edit"))
.andExpect(status().isOk())
.andExpect(view().name("owners/createOrUpdateOwnerForm"))
.andExpect(model().attributeExists("owner"));
verifyZeroInteractions(ownerService);
}
@Test
void processUpdateOwnerForm() throws Exception {
when(ownerService.save(ArgumentMatchers.any())).thenReturn(Owner.builder().id(1l).build());
mockMvc.perform(post("/owners/1/edit"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/owners/1"))
.andExpect(model().attributeExists("owner"));
verify(ownerService).save(ArgumentMatchers.any());
}
}
this my pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>tdm-pet-clinic</artifact Id>
<groupId>guru.springframework</group Id>
<version>0.0.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>pet-clinic-web</artifact Id>
<properties>
<!-- Web dependencies -->
<webjars-bootstrap.version>3.3.6</we bjars-boot strap.vers ion>
<webjars-jquery-ui.version>1.11.4</w ebjars-jqu ery-ui.ver sion>
<webjars-jquery.version>2.2.4</webja rs-jquery. version>
<wro4j.version>1.8.0</wro4j.version>
</properties>
<dependencies>
<dependency>
<artifactId>pet-clinic-data</artifac tId>
<groupId>guru.springframework</group Id>
<version>0.0.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</g roupId>
<artifactId>spring-boot-starter-actu ator</arti factId>
</dependency>
<dependency>
<groupId>org.springframework.boot</g roupId>
<artifactId>spring-boot-starter-thym eleaf</art ifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</g roupId>
<artifactId>spring-boot-starter-web< /artifactI d>
</dependency>
<dependency>
<groupId>org.springframework.boot</g roupId>
<artifactId>spring-boot-devtools</ar tifactId>
<scope>runtime</scope>
</dependency>
<!-- webjars -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</ar tifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>${webjars-jquery.version}</ version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery-ui</artifactId>
<version>${webjars-jquery-ui.version }</version >
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>${webjars-bootstrap.version }</version >
</dependency>
<!-- end of webjars -->
<dependency>
<groupId>org.springframework.boot</g roupId>
<artifactId>spring-boot-starter-test </artifact Id>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artif actId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</ar tifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</a rtifactId>
<version>2.22.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artif actId>
<version>5.5.0-RC1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId >
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</a rtifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupI d>
<artifactId>spring-test</artifactId>
<version>5.2.0.M3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</g roupId>
<artifactId>spring-boot-test</artifa ctId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>ro.isdc.wro4j</groupId>
<artifactId>wro4j-maven-plugin</arti factId>
<version>${wro4j.version}</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<wroManagerFactory>ro.isdc.wro.maven .plugin.ma nager.fact ory.Config urableWroM anagerFact ory</wroMa nagerFacto ry>
<cssDestinationFolder>${project.buil d.director y}/classes /static/re sources/cs s</cssDest inationFol der>
<wroFile>${basedir}/src/main/wro/wro .xml</wroF ile>
<extraConfigFile>${basedir}/src/main /wro/wro.p roperties< /extraConf igFile>
<contextFolder>${basedir}/src/main/l ess</conte xtFolder>
</configuration>
<dependencies>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>${webjars-bootstrap.version }</version >
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifa ctId>
<version>2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId >
<version>2.23.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
Are you still getting the same error after adding
import static org.hamcrest.Matchers.*;
?
ASKER
Yes same error
Have you tried my suggestion?
ASKER
I did add in pom I also send my pom
Can you do a mvn clean as well?
Ok, can you try with the 1.3 version?
<dependency>
<groupId>org.hamcrest</gro upId>
<artifactId>hamcrest-libra ry</artifa ctId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</gro
<artifactId>hamcrest-libra
<version>1.3</version>
<scope>test</scope>
</dependency>
ASKER
am geting this
[mvn compile
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for guru.springframework:pet-clinic-web: jar:0.0.5
[WARNING] 'dependencies.dependency.(groupId:ar tifactId:t ype:classi fier)' must be unique: org.junit.jupiter:junit-ju piter-api: jar -> version (?) vs
5.5.0-RC1 @ guru.springframework:pet-clinic-web: [unknown-v ersion], C:\spring5Clinic\pet-clini c-web\pom. xml, line 97, column 21
[WARNING] 'dependencies.dependency.(groupId:ar tifactId:t ype:classi fier)' must be unique: org.mockito:mockito-junit- jupiter:ja r -> version 2.22.0 vs
(?) @ guru.springframework:pet-clinic-web: [unknown-v ersion], C:\spring5Clinic\pet-clini c-web\pom. xml, line 107, column 21
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO] ------------------------------------ ---------- ---------- ---------- ------
[INFO] Reactor Build Order:
[INFO]
[INFO] tdm-pet-clinic [pom]
[INFO] pet-clinic-data [jar]
[INFO] pet-clinic-web [jar]
[INFO]
[INFO] ----------------< guru.springframework:tdm-pet-clinic >-----------------
[INFO] Building tdm-pet-clinic 0.0.5 [1/3]
[INFO] --------------------------------[ pom ]------------------------- --------
[INFO]
[INFO] ----------------< guru.springframework:pet-clinic-data >----------------
[INFO] Building pet-clinic-data 0.0.5 [2/3]
[INFO] --------------------------------[ jar ]------------------------- --------
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resourc es (default-resources) @ pet-clinic-data ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] Copying 14 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ pet-clinic-data ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 28 source files to C:\spring5Clinic\pet-clinic-data\tar get\classe s
[INFO]
[INFO] ----------------< guru.springframework:pet-clinic-web >-----------------
[INFO] Building pet-clinic-web 0.0.5 [3/3]
[INFO] --------------------------------[ jar ]------------------------- --------
[WARNING] The POM for org.springframework:spring-test:jar: 5.2.0.M3 is missing, no dependency information available
[INFO] ------------------------------------ ---------- ---------- ---------- ------
[INFO] Reactor Summary for tdm-pet-clinic 0.0.5:
[INFO]
[INFO] tdm-pet-clinic .................................... . SUCCESS [ 0.183 s]
[INFO] pet-clinic-data .................................... SUCCESS [ 10.553 s]
[INFO] pet-clinic-web .................................... . FAILURE [ 0.292 s]
[INFO] ------------------------------------ ---------- ---------- ---------- ------
[INFO] BUILD FAILURE
[INFO] ------------------------------------ ---------- ---------- ---------- ------
[INFO] Total time: 11.615 s
[INFO] Finished at: 2019-10-05T14:42:08+02:00
[INFO] ------------------------------------ ---------- ---------- ---------- ------
Failed to execute goal on project pet-clinic-web: Could not resolve dependencies for project guru.springframework:pet-clinic-web: jar:0.0.5:
Failure to find org.springframework:spring-test:jar: 5.2.0.M3 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolutio
n will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]
To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.
For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
After correcting the problems, you can resume the build with the command
mvn <goals> -rf :pet-clinic-web
Ok, can you post a small compilable example that demonstrates the problem? I will need to run it locally.
ASKER
check the code hear https://github.com/chalie86/tdm-pet-clinic.git
Can you tell me what command you're using to build the project? I downloaded it and built it locally here and I had no problems.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
From the limited information, I am going to guess that you are using "hamcrest" library to help with the testing. If so, try adding the following at the top of the file with the other import statements...
import static org.hamcrest.Matchers.*;
If this doesn't work, can you post the entire "OwnerControllerTest.java"