Skip to content
Advertisement

Spring Boot Fails while maven test …jdk8 and aws-sdk-bom 1.11

This is my pom.xml

4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE com.dummy lattt 0.0.1-SNAPSHOT war lattt lattt

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180813</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-bean-validators</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>dev.morphia.morphia</groupId>
        <artifactId>core</artifactId>
        <version>1.5.3</version>
    </dependency>
    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-oauth2</artifactId>
        <version>v2-rev150-1.25.0</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.2.11</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.2.11</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.11</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>20.0</version>
    </dependency>
    <dependency>
        <groupId>org.javers</groupId>
        <artifactId>javers-core</artifactId>
        <version>3.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.23.1-GA</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.17</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-secretsmanager</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- Mockito -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- PowerMock -->
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>2.0.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
        <version>2.0.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.sonarsource.scanner.maven</groupId>
        <artifactId>sonar-maven-plugin</artifactId>
        <version>3.2</version>
    </dependency>
    
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-bom</artifactId>
            <version>1.11.618</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.0</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

    </plugins>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.6.2</version>
        </extension>
    </extensions>
    <finalName>${project.artifactId}</finalName>
</build>
<profiles>
    <profile>
        <id>sonar</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- Optional URL to server. Default value is http://localhost:9000 -->
            <sonar.host.url>
                http://sonar.com
            </sonar.host.url>
            <sonar.login>
                dummy
            </sonar.login>

        </properties>
    </profile>
</profiles>

I have 2 junit test and these are failing repeatedly

1.Installed and Using jdk 8

2.But aws-sdk-bom runs with 1.11.618 and downloads&executes test with 1.11.618

3.And throws the error

10:16:49.904 [main] DEBUG com.amazonaws.AmazonWebServiceClient - Internal logging successfully configured to commons logger: true
10:16:49.913 [main] DEBUG com.amazonaws.metrics.AwsSdkMetrics - Admin mbean registered under com.amazonaws.management:type=AwsSdkMetrics
10:16:50.092 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.EnvironmentVariableCsmConfigurationProvider@2622fd05: Unable to load Client Side Monitoring configurations from environment variables!
10:16:50.093 [java-sdk-http-connection-reaper] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing connections idle longer than 60000 MILLISECONDS
10:16:50.093 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.SystemPropertyCsmConfigurationProvider@7e5ecc5f: Unable to load Client Side Monitoring configurations from system properties variables!
10:16:50.096 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.ProfileCsmConfigurationProvider@3b38e484: Unable to load config file
setup
[[1;31mERROR[m] [1;31mTests [0;1mrun: [0;1m8[m, Failures: 0, [1;31mErrors: [0;1;31m8[m, Skipped: 0, Time elapsed: 21.379 s[1;31m <<< FAILURE![m - in com.logi.bootstrap.controller.[1mStoreControllerTest[m
[[1;31mERROR[m] updateStoresByAdmin(com.logi.bootstrap.controller.StoreControllerTest)  Time elapsed: 0.77 s  <<< ERROR!
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.authentication.TestingAuthenticationToken
    at com.logi.bootstrap.controller.StoreControllerTest.updateStoresByAdmin(StoreControllerTest.java:155)
Caused by: org.springframework.security.authentication.ProviderNotFoundException: No AuthenticationProvider found for org.springframework.security.authentication.TestingAuthenticationToken
    at com.logi.bootstrap.controller.StoreControllerTest.updateStoresByAdmin(StoreControllerTest.java:155)

Test file

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore({ "javax.management.*", "org.apache.http.conn.ssl.*", "com.amazonaws.*", "javax.net.ssl.*",
        "com.mongodb.*", "dev.morphia.*","javax.security.*" })
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK, classes = LatApplication.class)
@WebAppConfiguration
@Import({TestConfig.class})
@PrepareForTest({ AdminChecker.class, StoreTable.class })
public class StoreControllerTest {
    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mvc;
    @MockBean
    StoreTable storeTbl;
    @MockBean
    RegionalAdminTable regionAdminTbl;
    @MockBean
    AccountServiceHandler accSrvHandler;

    @Before
    public void setup() {
        System.out.println("setup");
        mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();

    }
    

    @Test
    @WithMockUser(roles = { "administrator" },value="demo@example.com")
    public void getStoresByAdmin() throws Exception {
        // given
        // 308 store in the system
        PowerMockito.mockStatic(AdminChecker.class);
        JSONArray arr = new JSONArray();
        JSONObject result = new JSONObject();
        result.put("resultList", arr);
        PowerMockito.when(AdminChecker.checkIsAdmin(Mockito.any())).thenReturn(true);
        // when
        // admin get store list
        // then
        mvc.perform(MockMvcRequestBuilders.get("/stores").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk()).andExpect(jsonPath("$.resultList", hasSize(0)));
    }

    @Test
    @WithMockUser(roles = { "agent" })
    public void getStoresByAgent() throws Exception {
        // given
        // 308 store in the system
        PowerMockito.mockStatic(AdminChecker.class);
        JSONArray arr = new JSONArray();
        JSONObject result = new JSONObject();
        result.put("resultList", arr);
        PowerMockito.when(AdminChecker.checkIsAdmin(Mockito.any())).thenReturn(false);
        PowerMockito.when(accSrvHandler.getNameByEmail(Mockito.anyString())).thenReturn("test");
        // when
        // admin get store list
        // then
        mvc.perform(MockMvcRequestBuilders.get("/stores").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());
    }

    @Test
    @WithMockUser(roles = { "administrator" }, username = "myUser")
    //@WithUserDetails("sys@sys.com")
    public void createStoresByAdmin() throws Exception {
        // given
        // 308 store in the system
        ObjectMapper mapper = new ObjectMapper();
        Mockito.doNothing().when(storeTbl).save(Mockito.any());
        Authentication auth = Mockito.mock(Authentication.class);
        Mockito.when(auth.getName()).thenReturn("demo@example.com");
        // when
        Store newStore = new Store();
        // admin get store list
        // then
        // System.out.println(storeTbl);
        mvc.perform(MockMvcRequestBuilders.post("/stores").contentType(MediaType.APPLICATION_JSON)
                .content(mapper.writeValueAsString(newStore))).andExpect(status().isOk());
    }

    @Test
    @WithMockUser(roles = { "administrator" }, username = "myUser")
    // @WithUserDetails("sys@sys.com")
    public void createStoresFailByAdmin() throws Exception {
        // given
        // 308 store in the system
        ObjectMapper mapper = new ObjectMapper();
        Mockito.when(storeTbl.checkExist(Mockito.any(Store.class))).thenReturn(true);
        Mockito.doNothing().when(storeTbl).save(Mockito.any());
        Authentication auth = Mockito.mock(Authentication.class);
        Mockito.when(auth.getName()).thenReturn("demo@example.com");
        // when
        Store newStore = new Store();
        // admin get store list
        // then
        // System.out.println(storeTbl);
        mvc.perform(MockMvcRequestBuilders.post("/stores")
                .contentType(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(newStore)))
                .andExpect(status().isConflict());
    }

    @Test
    //@WithMockUser(roles = { "administrator" })
    @WithUserDetails("sys@sys.com")
    public void updateStoresByAdmin() throws Exception {
        // given
        // a store in the system
        ObjectMapper mapper = new ObjectMapper();
        Mockito.doNothing().when(storeTbl).save(Mockito.any());
        Mockito.when(storeTbl.getStoreById(Mockito.anyString())).thenReturn(new Store());
        // when
        Store newStore = new Store();
        // admin get store list
        // then
        // System.out.println(storeTbl);
        mvc.perform(MockMvcRequestBuilders.put("/stores/test").contentType(MediaType.APPLICATION_JSON)
                .content(mapper.writeValueAsString(newStore))).andExpect(status().isOk());
    }

    @Test
    @WithMockUser(roles = { "administrator" }, username = "myUser")
    public void updateStoresFailByAdmin() throws Exception {
        // given
        // a store in the system
        ObjectMapper mapper = new ObjectMapper();
        Mockito.doNothing().when(storeTbl).save(Mockito.any());
        Mockito.when(storeTbl.getStoreById(Mockito.anyString())).thenReturn(null);
        Authentication auth = Mockito.mock(Authentication.class);
        Mockito.when(auth.getName()).thenReturn("demo@example.com");
        // when
        Store newStore = new Store();
        // admin get store list
        // then
        // System.out.println(storeTbl);
        mvc.perform(MockMvcRequestBuilders.put("/stores/test").contentType(MediaType.APPLICATION_JSON)
                .content(mapper.writeValueAsString(newStore))).andExpect(status().isNotFound());
    }

    @Test
    @WithMockUser(roles = { "administrator" }, username = "myUser")
    public void deleteStoreByAdmin() throws Exception {
        // given
        // a store in the system
        ObjectMapper mapper = new ObjectMapper();
        Mockito.when(storeTbl.delete(Mockito.anyString())).thenReturn(true);
        Mockito.when(storeTbl.checkExist(Mockito.anyString())).thenReturn(true);
        // when
        Store newStore = new Store();
        // admin get store list
        // then
        // System.out.println(storeTbl);
        mvc.perform(MockMvcRequestBuilders.delete("/stores/test").contentType(MediaType.APPLICATION_JSON)
                .content(mapper.writeValueAsString(newStore))).andExpect(status().isOk());
    }

    @Test
    @WithMockUser(roles = { "administrator" }, username = "myUser")
    // @WithUserDetails("sys@sys.com")
    public void deleteStoreFailByAdmin() throws Exception {
        // given
        // a store in the system
        ObjectMapper mapper = new ObjectMapper();
        Mockito.when(storeTbl.delete(Mockito.anyString())).thenReturn(true);
        Mockito.when(storeTbl.checkExist(Mockito.anyString())).thenReturn(false);
        // when
        Store newStore = new Store();
        // admin get store list
        // then
        // System.out.println(storeTbl);
        mvc.perform(MockMvcRequestBuilders.delete("/stores/test").contentType(MediaType.APPLICATION_JSON)
                .content(mapper.writeValueAsString(newStore))).andExpect(status().is4xxClientError());
    }

}

How to execute the mvn test with errors

Authentication Provider ->Yes

Advertisement

Answer

Could not find artifact com.amazonaws:aws-java-sdk-bom:pom:2.15.4 in central

To address this POM issue, please refer to the AWS Spring BOOT example applications that are located in https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/usecases.

They all work and use AWS SDK For Java Version 2. I have deployed every one of them to the Cloud by using Elastic BeanStalk. Furthermore, these Spring Boot example apps interact with different AWS services like DynamoDB, Amazon RDS, Amazon S3, Amazon SES, Amazon Rekognition, etc.

Once you are successful getting the apps to work using V2, then you can build some tests

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement