Search code examples
javajunit

Why can't I use @Before annotation in my project? "Before cannot be resolved to a type"


I am trying to create a JUnit test class like this:

public class AppTest {

@Before
public void setUp() {
    // Create the application from the configuration
    ApplicationContext context = new AnnotationConfigApplicationContext( ApplicationConfig.class )
    // Look up the application service interface
    service = (TransferService) context.getBean(TransferService.class);
    }
}

The main problem is that I obtain an error on the @Before annotation on the setUp() method. It say to me that:

Before cannot be resolved to a type

It seems as it can't find the dependency for the @Before annotation.

This is the dependency that I have in my pom.xml file:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>

What am I missing? What is wrong? How can I fix this issue?


Solution

  • @Before is available from junit 4.x, Change to newer Version of Junit.

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>