Home > Software engineering >  Parameter 0 of constructor in web.PasswcController required a bean of type 'data.PasswcReposito
Parameter 0 of constructor in web.PasswcController required a bean of type 'data.PasswcReposito

Time:10-21

I am not sure what is wrong with my codes. I was trying to learn Spring Boot. But I am not able to run the application as i get the below error

***************************
APPLICATION FAILED TO START
***************************

Description:
Parameter 0 of constructor in com.projects.passwcontroller.web.PasswcController required a bean of type 'com.projects.passwcontroller.data.PasswcRepository' that could not be found.

Action:
Consider defining a bean of type 'com.projects.passwcontroller.data.PasswcRepository' in your configuration.

Structure

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.projects</groupId>
    <artifactId>passwcontroller</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>passwcontroller</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>19</java.version>
    </properties>
    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>10.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.12.0</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.7.4</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>18</source>
                <target>18</target>
            </configuration>
        </plugin>
    </plugins>
</build>

main.java.com.projects.passwcontroller.PasswcontrollerApplication.

package com.projects.passwcontroller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class PasswcontrollerApplication {

    public static void main(String[] args) {
        SpringApplication.run(PasswcontrollerApplication.class, args);
    }

}

main.java.com.projects.passwcontroller.data.PasswcRepository

 package com.projects.passwcontroller.data;

import com.projects.passwcontroller.Passwc;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PasswcRepository {
    List<Passwc> findPasswd(long max, int count);
}

main.java.com.projects.passwcontroller.web.PasswcContriller

    package com.projects.passwcontroller.web;

import com.projects.passwcontroller.data.PasswcRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/passwds")
public class PasswcController {
    private PasswcRepository passwcRepository;

    @Autowired
    public PasswcController(PasswcRepository passwcRepository) {
        this.passwcRepository = passwcRepository;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String passwds(Model model) {
        model.addAttribute("passwdList", passwcRepository.findPasswd(
                Long.MAX_VALUE, 20));
        return "passwds";
    }
}

main.java.com.projects.passwcontroller.Passwc

 package com.projects.passwcontroller;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.Date;

public class Passwc {
    private final Long id;
    private String nameOfResource;
    private final String passwd;
    private final Date date;

    public Passwc(String nameOfResource, String passwd) {
        this.id = null;
        this.nameOfResource = nameOfResource;
        this.passwd = passwd;
        this.date = new Date();
    }

    public Long getId() {
        return id;
    }

    public String getNameOfResource() {
        return nameOfResource;
    }

    public String getPasswd() {
        return passwd;
    }

    public Date getDate() {
        return date;
    }

    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, this, "id", "date");
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this, "id", "time");
    }
}

test.java.com.projects.passwcontroller.web.PasswcControllerTest

package com.projects.passwcontroller.web;

import com.projects.passwcontroller.Passwc;
import com.projects.passwcontroller.data.PasswcRepository;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.servlet.view.InternalResourceView;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.Matchers.hasItems;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;


public class PasswcControllerTest {
    @Test
    public void shouldShowPasswdList() throws Exception {
        List<Passwc> expectedPasswd = createPassList(20);

        PasswcRepository mockRepository = mock(PasswcRepository.class);
        when(mockRepository.findPasswd(Long.MAX_VALUE, 20)).thenReturn(expectedPasswd);

        PasswcController controller = new PasswcController(mockRepository);

        MockMvc mockMvc = standaloneSetup(controller).setSingleView(new InternalResourceView("/WEB-INF/views/passwds.jsp")).build();
        mockMvc.perform(get("/passwds"))
                .andExpect(view().name("passwds"))
                .andExpect(model().attributeExists("passwdList"))
                .andExpect(model().attribute("passwdList", hasItems(expectedPasswd.toArray())));
    }

    private List<Passwc> createPassList(int count){
        List<Passwc> passwds = new ArrayList<Passwc>();

        for (int i = 0; i < count; i  ) {
            passwds.add(new Passwc("GitHub", "new pass "   i));
        }
        return passwds;
    }
}

Test result

CodePudding user response:

I think you might need to add a dependency:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
   <version>2.7.2</version>
</dependency>

This also means you might be missing a couple of JPA annotations in Passwc. Take a look at https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa.

  • Related