2

I'm trying to write a simple unit test. This test function will call my service, which grabs info from the database, pushes it to a list and returns it. I've looked high and low in the debug logs to find what could be causing, but it seems nothing on the web is helping me out. I'm not too familiar with Spring Boot, but it seems surprising that all this effort and I am unable to get a simple unit test to pass.

Error Logs

http://text-share.com/view/fb0369c3

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; 

Example Test

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

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); } } 

Application

package com.algoq.algoq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @SpringBootApplication //@EnableScheduling public class AlgoQApplication { public static void main(String[] args) { SpringApplication.run(AlgoQApplication.class, args); } } 

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"> <modelVersion>4.0.0</modelVersion> <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.8</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>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> </project> 

2 Answers 2

4

The real cause of the exception is

java.lang.ClassNotFoundException: javax.xml.bind.JAXBException 

So I believe you need to add JAXBE library to your classpath (it was removed from JDK in java9 and I guess you are using java9)

Try to add this to your pom file

<!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> 
Sign up to request clarification or add additional context in comments.

2 Comments

I feel like this is getting super close.. I'm still getting an error, but it looks a lot better. I'm thinking this is a JPA error? 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)
I suppose some issues with @MockBean. I guess it is returning null, you need to setup it first, see example here dzone.com/articles/mockbean-spring-boots-missing-ingredient i.e. when(employeeRepository.findAll()) .thenReturn(new ArrayList());
1

JDK

/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java

pom.xml

1.8

Are you using java 8 or 9 ?

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

Issue with java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException ,

Run the application with Java 8 environement or Include the JAXB library for Java 9, JAXB API are not included in JAVA SE 9. Refer Deprecated, for removal: This API element is subject to removal in a future version.. Use --add-modules ( --add-modules java.xml.bind ) to add module in classpath. Jave has only deprecated and does not add javax.xml.bind module on classpath by default.

Otherwise include as dependency through maven.

<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.