0

I am currently trying that tutorial:

http://www.vogella.com/tutorials/Mockito/article.html

The Setup here starts with and that is not applicable

 @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

The Docu of JUnit says its deprecated and I couldn't find a docu how I can make mockito setting up with JUnit 5. Even that tutorial above is from April this year, the newest I found.

Question: I wonder if mockito is just incompatible to JUnit5? If not: How can I make it run? I found a similar question (How to use Mockito with JUnit5) and the answer from Nicolai is not working. He sugggest to use @Mock...yes, my IDE accepts it, BUT the Mocks are not working in tests like in the example below

Here is my test file (with no tests so far)

import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class Test{

    @Rule
    public MockitoRule mockitoRule = MockitoJUnit.rule();

    @Mock
    Type change;

    @Mock
    File file;

    @Test
     void testCTORExceptionWithNull() {
         Throwable exception = assertThrows(AssertionError.class, () -> {
            new Person(file, -1);
       });
      assertEquals("buffer size is negative", exception.getMessage());
   }

}

Instead of receiving "buffer size is negative" I get "no file attached". So the @Mock doesn't work?!?

and 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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>1</groupId>
    <artifactId>2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.7.22</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>
</project>

I know it would be faster to just use Junit4 instead of asking that question, but somehow it attracts me to solve problems :)

Edit:

I don't know why RPC marked that here as an exact duplicate of the link I shared above...Moreover the answer in that question is wrong, as I also stated here.

Community
  • 1
  • 1
PowerFlower
  • 1,619
  • 4
  • 18
  • 27
  • 1
    Hey, please read Nicolai answer in duplicate question again, you have to either: use the "@Mock-annotation and the corresponding call to MockitoAnnotations::initMocks to create mocks works" or use the "prototypical MockitorExtension that the JUnit team implemented". –  Apr 27 '17 at 12:02
  • well ok I expected actually some official instead of a workaround nearly 6 month ago :) but ok then I live with that. Regards – PowerFlower Apr 27 '17 at 12:16
  • 2
    @PowerFlower Once the Mockito issue #445 mentioned in that other answer is resolved (and I hear about it) I will update the answer but as it stands it is up to date. – Nicolai Parlog Apr 27 '17 at 13:01
  • 1
    @PowerFlower maybe you could then cast an upvote on Nicolai answer (if not already done) ;) –  Apr 27 '17 at 13:21

0 Answers0