Fix malfunctioning tests Fix testDoFilterWithExistingProjectAndPackage test by adding a missing line which adds a mock project to the cache. Without this line, the test will assume that the entire string used is a valid project which is not the case (The extra is meant to represent a package, not a project). This fix is based on the changes made on the parent commit. Fix testDoFilterWithNonExistingProject test by using empty option. Optional.of does not work with null values and will throw a NullPointerException when the argument is null. empty returns an empty Optional value best representing the test case of a non existing project. Bug: Issue 12454 Change-Id: I356751bec4ccb6f44226219c9ae7b41e7dc02cd7
diff --git a/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java b/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java index 39f01df..1bea7b6 100644 --- a/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java +++ b/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
@@ -91,7 +91,6 @@ unitUnderTest = new GoImportFilter(mockAnonProvider, mockPerms, mockProjectCache, PROD_URL); assertThat(unitUnderTest).isNotNull(); when(mockResponse.getOutputStream()).thenReturn(mockOutputStream); - when(mockAnonProvider.get()).thenReturn(mockAnon); when(mockPerms.user(mockAnon)).thenReturn(mockPermsWithUser); when(mockPermsWithUser.ref(any())).thenReturn(mockPermsForRef); @@ -171,6 +170,8 @@ public void testDoFilterWithExistingProjectAndPackage() throws Exception { when(mockRequest.getServletPath()).thenReturn("/" + PROJECT_NAME + "/my/package"); when(mockRequest.getParameter("go-get")).thenReturn("1"); + when(mockProjectCache.get(Project.nameKey(PROJECT_NAME))) + .thenReturn(Optional.of(mockProjectState)); when(mockPermsForRef.testOrFalse(RefPermission.READ)).thenReturn(false); unitUnderTest.doFilter(mockRequest, mockResponse, mockChain); verify(mockOutputStream, times(1)).write(response200(false, false)); @@ -198,7 +199,7 @@ public void testDoFilterWithNonExistingProject() throws Exception { when(mockRequest.getServletPath()).thenReturn("/" + PROJECT_NAME); when(mockRequest.getParameter("go-get")).thenReturn("1"); - when(mockProjectCache.get(any(Project.NameKey.class))).thenReturn(null); + when(mockProjectCache.get(any(Project.NameKey.class))).thenReturn(Optional.empty()); unitUnderTest.doFilter(mockRequest, mockResponse, mockChain); verify(mockOutputStream, times(1)).write(any(byte[].class)); verify(mockChain, times(0)).doFilter(mockRequest, mockResponse);