2017-12-15 2 views
0

Ich versuche, einige Unit-Tests in meinem Frühjahr Boot-Anwendung und bekomme die folgenden Fehler auszuführen:JPA Verursachung java.lang.NullPointerException im Frühjahr Boot-

Ich kann nicht sagen, ob dies ist, weil es nicht kann Verbindung zu meiner Datenbank in Heroku (ClearDB) oder wenn ich einige Annotationen falsch in meinem Code habe.

Ich habe die Umgebungsvariablen zur Laufzeit in meine Datei application-dev.properties geladen, alle diese Informationen können am unteren Rand gesehen werden.

Fehler

java.lang.NullPointerException 
    at com.algoq.algoq.services.AlgorithmService.getSubscribers(AlgorithmService.java:26) 
    at com.algoq.algoq.ExampleTest.subscriberListNull(ExampleTest.java:42) 
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 

Testklasse

package com.algoq.algoq; 
import com.algoq.algoq.models.Subscriber; 
import com.algoq.algoq.respositories.SubscriberRepository; 
import com.algoq.algoq.services.AlgorithmService; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.boot.test.context.TestConfiguration; 
import org.springframework.boot.test.mock.mockito.MockBean; 
import org.springframework.context.annotation.Bean; 
import org.springframework.test.context.junit4.SpringRunner; 


import java.util.ArrayList; 

import static org.assertj.core.api.Assertions.assertThat; 

@RunWith(SpringRunner.class) 
@TestConfiguration 
@SpringBootTest(classes = { 
     AlgoQApplication.class, 
     }) 
public class ExampleTest extends AlgoQApplicationTests { 

    @Autowired 
    private AlgorithmService aService; 

    @MockBean 
    private SubscriberRepository employeeRepository; 

    @Bean 
    public AlgorithmService aService() { 
     return new AlgorithmService(); 
    } 


    @Test 
    public void subscriberListNull() throws Exception { 
     ArrayList<Subscriber> subs = aService.getSubscribers(); 
     assertThat(subs).isEmpty(); 
    } 

} 

Service Class

package com.algoq.algoq.services; 

import com.algoq.algoq.models.Subscriber; 
import com.algoq.algoq.respositories.SubscriberRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.stereotype.Service; 

import java.lang.reflect.Array; 
import java.util.ArrayList; 
import java.util.List; 

@Service 
public class AlgorithmService { 

    @Autowired 
    private SubscriberRepository subRep; 

    /** 
    * Gets a list of subscribers to return to the API 
    * @return 
    */ 
    public ArrayList<Subscriber> getSubscribers() { 
     ArrayList<Subscriber> subscribers = new ArrayList<>(); 
     subRep.findAll() 
       .forEach(subscribers::add); 
     return subscribers; 
    } 

    /** 
    * Adds a new subscriber to the database 
    * @param sub 
    * @return 
    */ 
    public void addSubscriber(Subscriber sub) { 
     subRep.save(sub); 
    } 

    /** 
    * Finds a single user id 
    * @param email 
    * @return 
    */ 
    public List<Subscriber> getSubscriber(String email) { 
     return subRep.findByEmailAddress(email); 
    } 
} 

POM

http://maven.apache.org/xsd/maven- 4.0.0.xsd "> 4.0.0

<groupId>com.algoQ</groupId> 
<artifactId>algo-q</artifactId> 
<version>0.0.1-SNAPSHOT</version> 
<packaging>jar</packaging> 

<name>algo-q</name> 
<description>An algorithm a day keeps the brain up to date</description> 

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.9.RELEASE</version> 
    <relativePath/> <!-- lookup parent from repository --> 
</parent> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    <java.version>1.9</java.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-rest</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-mail</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.itextpdf</groupId> 
     <artifactId>itextpdf</artifactId> 
     <version>5.5.12</version> 
    </dependency> 
    <dependency> 
     <groupId>com.itextpdf.tool</groupId> 
     <artifactId>xmlworker</artifactId> 
     <version>5.5.12</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.python</groupId> 
     <artifactId>jython-standalone</artifactId> 
     <version>2.5.3</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.xml.bind</groupId> 
     <artifactId>jaxb-api</artifactId> 
     <version>2.3.0</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/com.h2database/h2 --> 
    <!--<dependency>--> 
     <!--<groupId>com.h2database</groupId>--> 
     <!--<artifactId>h2</artifactId>--> 
     <!--<version>1.4.196</version>--> 
     <!--<scope>test</scope>--> 
    <!--</dependency>--> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
     <version>1.5.2.RELEASE</version> 
    </dependency> 
    <!--<dependency>--> 
     <!--<groupId>com.h2database</groupId>--> 
     <!--<artifactId>h2</artifactId>--> 
     <!--<version>1.4.194</version>--> 
    <!--</dependency>--> 
    <dependency> 
     <groupId>org.pygments</groupId> 
     <artifactId>pygments</artifactId> 
     <version>1.5</version> 
     <scope>runtime</scope> 
    </dependency> 
</dependencies> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 

App Eigenschaften

server.port = 5600 

spring.mail.host=smtp.gmail.com 
spring.mail.port=587 
spring.mail.username=${MAIL_USER} 
spring.mail.password=${MAIL_PASS} 

#mail properties 
spring.mail.properties.mail.smtp.auth=true 
spring.mail.properties.mail.smtp.starttls.enable=true 

spring.thymeleaf.cache=false 
spring.thymeleaf.mode=HTML5 

spring.datasource.driver-class-name=com.mysql.jdbc.Driver 
spring.datasource.url = ${MYSQL_HOST} 
spring.datasource.username = ${MYSQL_USERNAME} 
spring.datasource.password = ${MYSQL_PASSWORD} 
spring.datasource.tomcat.max-active=20 
spring.datasource.maxIdle=2 
spring.datasource.tomcat.remove-abandoned=true 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect 

spring.jpa.hibernate.ddl-auto=update 
+0

Sie verwenden einen Schein, das Standardverhalten für einen Schein ist die Rückgabe von 'null'. Der Aufruf von 'findAll 'gibt' null 'zurück, daher eine' NullPointerException' in 'forEach'. Hören Sie auch auf, Spring-Boot-Gläser aus verschiedenen Versionen '1.5.9' und' 1.5.2' zu mischen. –

+0

danke @ M.Deinum Können Sie das Versionierungsproblem näher erläutern? Wie kann ich das beheben –

+1

Entfernen Sie die Version aus der 'spring-data-jpa-Starter'-Abhängigkeit, und Sie können die' Ruhezustand-Kern'-Abhängigkeit entfernen. –

Antwort

4

Sie @MockBean verwenden, die unterhalb, Mockito.mock verspotteten Bean erstellen verwendet. Das Standardverhalten eines Mocks besteht darin, Standardwerte zurückzugeben. In diesem Fall wird null als Standardwert zurückgegeben. Daher wird es auf

brechen Was Sie tun müssen, ist Ihre Spott sagen, was bei einem bestimmten Methodenaufruf zu tun ist. In Ihrem Fall möchten Sie vielleicht eine leere Liste zurückgeben. In Ihrer Testmethode müssten Sie etwas in diese Richtung hinzufügen.

@Test 
public void subscriberListNull() throws Exception { 
    Mockito.when(employeeService.findAll)).thenReturn(new ArrayList()); 
    ArrayList<Subscriber> subs = aService.getSubscribers(); 
    assertThat(subs).isEmpty(); 
} 

Dies ist auch inline mit dem Standard für Spring Data JPA wie nie null aus einer Sammlung Rückkehr Methode zurück. Wenn es keine Ergebnisse gibt, erhalten Sie eine leere Sammlung.